So I am performing some simple webscraping using Cheerio. It's working great and the output is appearing in my cmd line. Can I get the data from the command line and put it into some JavaScript somehow? I basically want to recreate this scraped data on another webpage.
var request = require('request');
var cheerio = require('cheerio');
request('https://news.ycombinator.com', function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('span.comhead').each(function(i, element){
var a = $(this).prev();
console.log(a.text());
});
}
});
Any suggestions would be very appreciated.
You can append the data to the page that is loading the JavaScript.
<html>
<body>
<ul id="results">
</ul>
</body>
</html>
replace
console.log(a.text());
with
$("#results").append('<li>' + a.text() + '</li>');
Based on your comment:
I see what you mean. I just researched cheerio
and found that you can load the html that I provided into a variable.
//outside of the each, declare $results
var $results = cheerio.load('<html><body><ul id="results"></ul></body></html>');
//instead of the first append I gave (above), use this
$results.append('<li>' + a.text() + '</li>');
then you can output the resulting html to the browser with something like
res.send($results.html());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.