Nodejs抓取html页面内容(推荐)
废话不多说,直接给大家贴node.js抓取html页面内容的核心代码了。
具体代码如下所示:
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); });
相关推荐
swazerz 2020-05-16
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23