Advertisement
CLooker

Untitled

Jan 31st, 2018
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.get("/api/fetch", (req, res) => {
  2.     return axios.get("http://www.nationalreview.com/").then(response => {
  3.         const $ = cheerio.load(response.data);
  4.         let articles = [];
  5.  
  6.         $(".homepage_meta").each(function(i, element) {
  7.             let result = {};
  8.             result.title = $(this)
  9.                 .children(".homepage_item_title")
  10.                 .text();
  11.             result.summary = $(this)
  12.                 .children(".homepage_item_body")
  13.                 .text();
  14.             result.url = `www.nationalreview.com/${element.parent.attribs.href}`;
  15.             result.saved = false;
  16.             articles.push(result);
  17.         });
  18.  
  19.         db.Article.create(articles).catch(err => console.log("err: ", err));
  20.  
  21.         res.send({
  22.             message: "Scrape Complete"
  23.         });
  24.     });
  25. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement