Skip to content

Instantly share code, notes, and snippets.

@jsvine
Created May 21, 2017 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsvine/c537ac9509e7d0ed713cced4992faf39 to your computer and use it in GitHub Desktop.
Save jsvine/c537ac9509e7d0ed713cced4992faf39 to your computer and use it in GitHub Desktop.
Downloading CraftCans.com's canned beer database as structured data
// Step 1: Go to http://craftcans.com/db.php?search=all&sort=beerid&ord=desc&view=text
// Step 2: Open your browser's developer console
// Step 3: Paste the code below into the console
(function () {
var getText = function (el) {
return el.textContent.trim().replace(/\n/g, " ");
};
// Get the last <table> on the page
var tables = document.querySelectorAll("table");
var table = tables[tables.length - 1];
// Extract the text from each cell, for each row
var rows = [].slice.call(table.querySelectorAll("tr"));
var data = rows.slice(1).map(function (row) {
var cells = row.querySelectorAll("td");
return [].slice.call(cells).map(getText);
});
// Grab the header
var header = [].slice.call(rows[0].childNodes).map(getText);
// Join it all into a tab-separated values file
var tsv = [ header ].concat(data).map(function (row) {
return row.join("\t");
}).join("\n");
// Download the file via a data URI
var a = document.createElement("a");
a.href = "data:text/plain;charset=utf-8," + encodeURIComponent(tsv);
a.download = "craftcans.tsv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment