I am trying to scrape some info from a website and return it in an API. The object, RESULT, is being logged in the terminal, but when i go to /api/scrape, the JSON object is empty...
router.post("/api/scrape", function(req,res){
var result = [];
request("https://www.reddit.com/", function(error, response, html) {
// Load the body of the HTML into cheerio
var $ = cheerio.load(html);
// Empty array to save our scraped data
// With cheerio, find each h4-tag with the class "headline-link"
$(".entry").each(function(i, element) {
// Save the text of the h4-tag as "title"
var title = $(this).text();
// Find the h4 tag's parent a-tag, and save it's href value as "link"
var link = $(element).children("a").text();
// For each h4-tag, make an object with data we scraped and push it to the result array
result.push({
title: title,
link: link
});
});
// After the program scans each h4.headline-link, log the result
console.log(result);
return result;
});
res.json(result);
})
via Patrick Bentley
No comments:
Post a Comment