Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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');
Add Comment
Please, Sign In to add comment