Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created November 6, 2018 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiangenco/ccea29b34e05adf16a1cd26ba8b2c048 to your computer and use it in GitHub Desktop.
Save christiangenco/ccea29b34e05adf16a1cd26ba8b2c048 to your computer and use it in GitHub Desktop.
Download a wikipedia table as a .json file. Requires jquery and artoo.
function tableToJson(table) {
var data = [];
// first row needs to be headers
var headers = [];
for (var i=0; i<table.rows[0].cells.length; i++) {
headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
}
// go through cells
for (var i=1; i<table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
rowData[ headers[j] ] = tableRow.cells[j].innerText;
}
data.push(rowData);
}
return data;
}
var data = {};
$("table.wikitable").each(function(i,table){
var json = tableToJson(table);
$.each(json, function(i,row){
if(data[row.name]){
$.extend(data[row.name] || {}, row);
}else{
data[row.name] = row;
}
})
})
artoo.saveCsv($.map(data, function(e){return e}))
// tableToJson()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment