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 characters
// sync_scrape.js (tested with node 11.3) | |
const request = require("sync-request"); | |
const fetchUrl = url => { | |
console.time(`fetchUrl(${url})`); | |
const html = request("GET", url).getBody(); | |
console.timeEnd(`fetchUrl(${url})`); | |
return html; | |
}; | |
const scrapeData = html => { | |
const re = /href="([^"]+)"/g; | |
const hrefs = []; | |
let m; | |
while ((m = re.exec(html))) hrefs.push(m[1]); | |
return hrefs; | |
}; | |
const urls = [ | |
"http://neverssl.com/", | |
"https://www.ietf.org/rfc/rfc2616.txt", | |
"https://en.wikipedia.org/wiki/Asynchronous_I/O" | |
]; | |
const extactedData = {}; | |
async function main() { | |
console.time("elapsed"); | |
for (const url of urls) { | |
const html = await fetchUrl(url); | |
extactedData[url] = scrapeData(html); | |
} | |
console.log("> extracted data:", extactedData); | |
console.timeEnd("elapsed"); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment