Created
November 3, 2020 09:37
-
-
Save fiddlerwoaroof/17dc4862580229469c200fe9de21ca05 to your computer and use it in GitHub Desktop.
Reactive HTML parsing
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
const ch = require("cheerio"); | |
const fetch = require("node-fetch").default; | |
const getLeadingNum = (v) => parseInt(v.split(" ")[0], 10); | |
function HNParser(html) { | |
if (! (this instanceof HNParser)) { | |
return new HNParser(html); | |
} | |
this.html = html; | |
} | |
HNParser.prototype = { | |
get doc() { | |
return ch(this.html); | |
}, | |
get hnmain() { | |
return ch(this.doc.find("#hnmain")[0]); | |
}, | |
get body() { | |
return this.hnmain.find(".itemlist tr.athing"); | |
}, | |
get titles() { | |
return this.body.map(function () { | |
const cheery = ch(this); | |
const nextEl = cheery.next(); | |
const storylink = cheery.find(".title .storylink"); | |
return { | |
href: storylink.attr("href"), | |
title: storylink.text(), | |
score: getLeadingNum(nextEl.find(".score").text()), | |
age: getLeadingNum(nextEl.find(".age").text()), | |
comments: getLeadingNum( | |
nextEl.find(".age").next().next().next().text() | |
), | |
}; | |
}); | |
}, | |
}; | |
fetch("https://news.ycombinator.com") | |
.then((v) => v.text()) | |
.then(HNParser) | |
.then(({titles}) => | |
titles.each(function () { | |
const { href, title, score, age, comments } = this; | |
console.log(`${title} | |
${href} | |
score: ${score} age: ${age} comments: ${comments}`); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment