直到最近,用 Nightmare 进行抓取一直是一件轻而易举的事,我开始遇到没有详细信息和标题“导航错误”的错误 以及错误代码 118,如下所示。
{ [Error: navigation error]
“0”: { message: '导航错误', 代码:-118, 细节: '', 网址:'http://markets.ft.com/research/Browse-Companies '}, 长度:1, 错误: [ { message: '导航错误', 代码:-118, 细节: '', 网址:'http://markets.ft.com/research/Browse-Companies ' } ] }
我的 Nightmare 代码(Node.Js):
function *run(){
var nightmare = Nightmare({show : true });
nightmare.useragent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
var url = "http://markets.ft.com/research/Browse-Companies";
var industry = [];
//fetching data in { INDUSTRY , LINK , SECTOR } format .
yield nightmare.goto(url).inject('js' , './jquery-2.2.3.min.js').wait('#wsod').evaluate(function () {
var arr = [];
$('.simpleLinkList.wsodModuleContent li').each(function(){
arr.push({SECTOR : $(this).parents('ul').prev().text().replace('Sectors & Industries' , '') ,
INDUSTRY : $(this).text() , LINK : $(this).find('a').attr('href')});
})
return arr;
}).then(function (data) {
industry = data;
});
//using {LINK}
var companies = [];
for(var i = 0 ; i<2; i++)
{
yield nightmare.goto(industry[i].LINK).inject('js' , './jquery-2.2.3.min.js').wait('#wsod');
var nextExists = yield nightmare.visible('.wsod-icon-paging-next-active');
var maxpage = 3;
var currentpage = 1;
var data = []; /* Object({ Name: "" , Link : ""})*/
while(nextExists && currentpage < maxpage)
{
//pagination / checking if next page exists and looping the scraper for each page
yield nightmare.evaluate(function(a , b){
var obj = [];
$('.company-link').each(function () {
obj.push({Sector : a , Industry: b , Name: $(this).text() , Link: $(this).attr('href')});
});
return obj;
},industry[i].SECTOR , industry[i].INDUSTRY).then(function (obj) {
data.push(obj);
});
yield nightmare.click('.wsod-icon-paging-next-active').wait(2000);
currentpage++;
nextExists = yield nightmare.visible('.wsod-icon-paging-next-active');
}
//data is an array of arrays and needs to be flattened.
var x = [].concat.apply([] , data);
//now pushing data to companies list (entire container)
companies.push(x);
}
companies = [].concat().apply([], companies);
//now companies is an array of entire list of all companies in every single sector->industry with sector name included for ease
console.log(companies);*/
console.log(companies);
yield nightmare.end();
}
如果有人可以提供有关此错误的更多信息,那就太好了。该程序有时可以工作,但大多数时候我都会收到“导航错误”
最佳答案
好吧,经过对 chromium 文档的一些研究后,它为 Electron+Nightmare 提供了动力。
错误代码 -118 与超时有关,但是在真正的 chromium 浏览器上立即加载的网站上会弹出此问题。
目前看起来像是一个 Electron 错误,如果有人了解更多信息,请提供详细信息。
关于node.js - Nightmare/Electron : Navigation Error (code - 118),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37144314/