I'm trying to get all links of the articles of one blog ( https://www.mrmoneymustache.com ) so I can compile them into a pdf, but i'm a complete noob in javascript. Somebody on reddit told me to use this code, which is supposed to do what I want :
const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const e = new EventEmitter();
e.on('fetchPage', link => {
fetch(link).then(r => r.text()).then(cheerio.load).then($ => {
const nextLink = $(".next_post a").attr('href');
if (nextLink === undefined) return; // end on final page
const postTitle = $(".headline").text();
const postContent = $(".post_content").html();
console.log(postTitle);
fs.writeFileSync(postTitle + ".html", postContent);
setTimeout(() => e.emit('fetchPage', nextLink), 5000);
});
});
e.emit('fetchPage', 'https://whatever/post1');
But I dont really get how I am supposed to run this program.. Help please ?