Skip to content

Instantly share code, notes, and snippets.

@Hyllesen
Last active January 27, 2020 14:25
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 Hyllesen/f8fbfc9539c7d4fa2af4d1c2af25ee60 to your computer and use it in GitHub Desktop.
Save Hyllesen/f8fbfc9539c7d4fa2af4d1c2af25ee60 to your computer and use it in GitHub Desktop.
const request = require("request-promise");
const cheerio = require("cheerio");
async function main() {
const result = await request.get("http://codingwithstefan.com/table-example");
const $ = cheerio.load(result);
const scrapedData = [];
const tableHeaders = [];
$("body > table > tbody > tr").each((index, element) => {
if (index === 0) {
const ths = $(element).find("th");
$(ths).each((i, element) => {
tableHeaders.push(
$(element)
.text()
.toLowerCase()
);
});
return true;
}
const tds = $(element).find("td");
const tableRow = {};
$(tds).each((i, element) => {
tableRow[tableHeaders[i]] = $(element).text();
});
scrapedData.push(tableRow);
});
console.log(scrapedData);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment