Skip to content

Instantly share code, notes, and snippets.

@mtorchiano
Last active June 3, 2019 13:46
Show Gist options
  • Save mtorchiano/48d2597cbd62487d8986938545f53354 to your computer and use it in GitHub Desktop.
Save mtorchiano/48d2597cbd62487d8986938545f53354 to your computer and use it in GitHub Desktop.
Bookmarklet for scraping Italian election official website
javascript:(function() {
let res = [];
let table = $('#superTable tr');
let headings = table.eq(0).children().map( (i,x) => x.innerText ).get();
let dups = {};
headings = headings.map( e => {
let n = dups[e] | 0;
let ee = n ? e + n : e;
dups[e] = n + 1;
return ee;
});
res = table.slice(1).map( (i,r) =>
$(r).children().map((i, x) => x.innerText)
).get().map( r => {
let res = {};
r.get().forEach( (e,i) => res[headings[i]] = e);
return res;
});
let output=window.open("","Output window","width=600,height=600,location:no,toolbar:no");
output.document.write("<head><title>Table contents</title></head><body><pre>\n");
output.document.write(JSON.stringify(res).replace(/},/g,"},\n"));
output.document.write("</pre></body>");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment