【问题标题】:HTTP Get Node JS to parse using document elementsHTTP Get Node JS 使用文档元素进行解析
【发布时间】:2015-03-28 05:39:54
【问题描述】:

我已经编写了一些 javascript 来访问页面并从站点返回数据,但是,我想从这个 html 站点中获取特定元素并使用 document.getElementById 之类的函数。我怎样才能在这里使用这种功能?目前,console.log(chunk) 只是吐出整个 html 正文,我希望能够解析它。

var http = require("http");

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/news'
};

http.get(options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function(chunk){
        console.log(chunk);

    });

}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

【问题讨论】:

  • 欢迎来到 Stack Overflow。我们不是代码生产!请向我们展示您的尝试!
  • 我试过了。我所能做的就是打印正文,但不确定如何选择特定元素。
  • 你已经说过如何:getElementById

标签: javascript html node.js http


【解决方案1】:

有许多 npm 模块可以执行相同的操作,这里有一些

1.cheerio

2.jsdom

【讨论】:

    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2021-04-29
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多