时间:2021-07-01 10:21:17 帮助过:102人阅读
具体代码如下所示:
var http = require("http"); var iconv = require('iconv-lite'); var option = { hostname: "stockdata.stock.hexun.com", path: "/gszl/s601398.shtml" }; var req = http.request(option, function(res) { res.on("data", function(chunk) { console.log(iconv.decode(chunk, "gbk")); }); }).on("error", function(e) { console.log(e.message); }); req.end();
下面再看下面一种nodejs抓取网页内容
function loadPage(url) { var http = require('http'); var pm = new Promise(function (resolve, reject) { http.get(url, function (res) { var html = ''; res.on('data', function (d) { html += d.toString() }); res.on('end', function () { resolve(html); }); }).on('error', function (e) { reject(e) }); }); return pm; } loadPage('http://www.baidu.com').then(function (d) { console.log(d); });
更多Nodejs抓取html页面内容相关文章请关注PHP中文网!