无论如何,我可以在node.js中获得html正文
我尝试向Apache服务器发送请求并使用返回的主体。
按照nodejs的手册http://nodejs.org/api/https.html#https_https_request_options_callback
和相关的SO内容如何使用Node.js创build外部HTTP请求
我的src是
callback = function(response) { body=''; console.log('HEADERS: ' + JSON.stringify(response.headers)); response.on('data', function (chunk) { body+= chunk; }); response.on('end', function () { console.log(body); }); }
我的问题是,这里的身体将包括所有的HTML头标记,如<!DOCTYPE html>
…等,不能被JSON对象parsing。 因为我所有的数据在HTML体内都是JSOn,所以我只想得到HTML正文。 无论如何要实现这个目标? 提前致谢。
我在这里没有给出完整的代码。
-
您可以使用像
htmlparser2
这样的模块从HTML的正文中提取数据(Online demo for the same
)。 -
然后,您可以使用
JSON.parse
将提取的stringparsing为JSON对象。