【发布时间】:2018-02-28 02:37:34
【问题描述】:
我尝试抓取一个简单的页面(需要cheerio 和请求): https://www.ishares.com/uk/individual/en/products/251824/
代码失败。我相信这是因为,为了达到上述目的,用户会在上一页被提示输入“个人”或“机构”,因此被重定向。
我尝试了不同的 url 变体,但都失败了。
如何使用 node.js 获取原始 HTML?
代码如下:
var express = require('express');
var path = require('path');
var request = require('request');
var cheerio = require('cheerio'); // fast flexible implement of jQuery for server.
var fs = require('fs');
var app = express();
var port = 8000;
var timeLog = []; // for dl to measure the time of events.
// var startTime = Date.now();
timeLog[0] = Date.now();
console.log('program initiated at time: '+new Date());
// example 1: pull the webpage and print to console
var url ="https://www.ishares.com/uk/individual/en/products/251824/ishares-jp-morgan-emerging-markets-bond-ucits-etf";
url = "https://www.ishares.com/uk/individual/en/products/251824/";
url="https://www.ishares.com/uk/individual/en/products/251824/ishares-jp-morgan-emerging-markets-bond-ucits-etf?siteEntryPassthrough=true&locale=en_GB&userType=individual";
request(url,function functionName(err,resp,body) {
var $ = cheerio.load(body);
var distYield = $('.col-distYield');
var distYieldText = distYield.text();
console.log('we got to line 24');
console.log(distYieldText);
timeLog[2] = Date.now();
console.log('data capture time: '+(timeLog[2] - timeLog[0])/1000+' seconds');
if (err) {
console.log(err);
}else {
//console.log(body);
console.log('the body was written: success');
}
});
// example 2: download webpage and save file
var destination = fs.createWriteStream('./downloads/iSharesSEMB.html');
request(url)
.pipe(destination);
// example 3:
var destination = fs.createWriteStream('./downloads/iSharesSEMB2.html');
request(url)
.pipe(destination)
.on("finish",function () {
console.log('done');
})
.on('error',function (err) {
console.log(err);
});
timeLog[1] = Date.now();
console.log('program completed at time: '+new Date());
console.log('Asynchronous program run time: '+(timeLog[1] - timeLog[0])/1000+' seconds');
【问题讨论】:
-
您是否遇到错误或只是未能抓取您想要的 HTML 部分?你到底想刮什么部位?
-
没有收到错误。实际发生的是获取第一页的 HTML,而不是包含所需信息的重定向页面(在回答问题后)(例如在该页面上,分发收益)。可以在此处查看输出 HTML:var destination = fs.createWriteStream('./downloads/iSharesSEMB.html');