문제

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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top