Last active
January 27, 2020 14:25
Revisions
-
Hyllesen revised this gist
Jan 27, 2020 . 2 changed files with 31 additions and 17 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ 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(); This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +0,0 @@ -
Hyllesen revised this gist
Jan 27, 2020 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,3 @@ async function main() { const result = await request.get("http://codingwithstefan.com/table-example"); const $ = cheerio.load(result); @@ -17,4 +14,4 @@ async function main() { console.log(scrapedData); } main(); -
Hyllesen created this gist
Jan 27, 2020 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ 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 = []; $("body > table > tbody > tr").each((index, element) => { if (index === 0) return true; const tds = $(element).find("td"); const company = $(tds[0]).text(); const contact = $(tds[1]).text(); const country = $(tds[2]).text(); const tableRow = { company, contact, country }; scrapedData.push(tableRow); }); console.log(scrapedData); } main();