Skip to content

Instantly share code, notes, and snippets.

@Nabeelhassan
Last active March 23, 2020 19:44
Show Gist options
  • Select an option

  • Save Nabeelhassan/248da2eef15182d3479a31c69393f8ec to your computer and use it in GitHub Desktop.

Select an option

Save Nabeelhassan/248da2eef15182d3479a31c69393f8ec to your computer and use it in GitHub Desktop.
wikiepdia: asia table data extractor
// This gist extracts the 'Political geography' Table From https://en.wikipedia.org/wiki/Asia
// Political geography table index
table = document.querySelectorAll('.wikitable')[2];
header = Array.from(table.rows[0].cells).map(i => i.innerText);
Flag = 0;
Symbol = 1;
data = [];
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows
const obj = {};
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns
obj[header[j]] = [Flag].includes(j) ? col.firstChild.firstChild.href : [Symbol].includes(j) ? col.firstChild.href : col.innerText;
}
data.push(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment