titleNodeList Loop puppeteer
var news = await page.evaluate(() => {
var titleNodeList = document.querySelectorAll(`a.storylink`);
var ageList = document.querySelectorAll(`span.age`);
var scoreList = document.querySelectorAll(`span.score`);
var titleLinkArray = [];
for (var i = 0; i < titleNodeList.length; i++) {
titleLinkArray[i] = {
title: titleNodeList[i].innerText.trim(),
link: titleNodeList[i].getAttribute("href"),
age: ageList[i].innerText.trim(),
score: scoreList[i].innerText.trim()
};
}
return titleLinkArray;
});
fs.writeFile("hackernews.json", JSON.stringify(news), function(err) {
if (err) throw err;
console.log("Saved!");
});