这篇文章主要介绍了 Nodejs 抓取 html 页面内容的关键代码,另外还给大家附上了 nodejs 抓取网页内容,非常不错,对 node.js 抓取页面内容感兴趣的朋友一起学习吧
Node.js 是一个基于 Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用 · Node.js 借助事件驱动, 非阻塞 I/O 模型变得轻量和高效, 非常适合 运行在分布式设备 的 数据密集型 的实时应用
废话不多说,直接给大家贴 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);
- });
来源: http://www.phperz.com/article/17/0317/263641.html