繁体   English   中英

在网页的cmd行中显示数据

[英]Displaying data in cmd line on a webpage

因此,我正在使用Cheerio进行一些简单的网络抓取。 它工作得很好,输出出现在我的cmd行中。 我可以从命令行获取数据,然后以某种方式将其放入一些JavaScript吗? 我基本上想在另一个网页上重新创建此抓取的数据。

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());
  });
 }
});

任何建议将不胜感激。

您可以将数据附加到正在加载JavaScript的页面上。

<html>
<body>
<ul id="results">
</ul>
</body>
</html>

更换

console.log(a.text());

$("#results").append('<li>' + a.text() + '</li>');

根据您的评论:

我明白你的意思了。 我刚刚研究了cheerio ,发现可以将提供的html加载到变量中。

//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>');

然后您可以使用类似的方法将结果html输出到浏览器

res.send($results.html());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM