Guest User

Untitled

a guest
Apr 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. const fs = require('fs');
  2. const EventEmitter = require('events').EventEmitter;
  3. const fetch = require('node-fetch');
  4. const cheerio = require('cheerio');
  5.  
  6. const e = new EventEmitter();
  7.  
  8. e.on('fetchPage', link => {
  9. fetch(link).then(r => r.text()).then(cheerio.load).then($ => {
  10. const nextLink = $(".next_post a").attr('href');
  11. if (nextLink === undefined) return; // end on final page
  12. const postTitle = $(".headline").text();
  13. const postContent = $(".post_content").html();
  14. console.log(postTitle);
  15. fs.writeFileSync(postTitle + ".html", postContent);
  16. setTimeout(() => e.emit('fetchPage', nextLink), 5000);
  17. });
  18. });
  19.  
  20. e.emit('fetchPage', 'https://whatever/post1');
Add Comment
Please, Sign In to add comment