Skip to content

Instantly share code, notes, and snippets.

@fiddlerwoaroof
Created November 3, 2020 09:37
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 fiddlerwoaroof/17dc4862580229469c200fe9de21ca05 to your computer and use it in GitHub Desktop.
Save fiddlerwoaroof/17dc4862580229469c200fe9de21ca05 to your computer and use it in GitHub Desktop.
Reactive HTML parsing
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