【发布时间】:2014-08-19 19:46:10
【问题描述】:
我目前正在编写一个涉及一些网络抓取的网络应用程序。为了解决这个问题,我正在使用 phantomjs 的帮助。但是,某些(但不是全部)网页返回 status="fail"。
这是代码(注意:这实际上是用 nodejs 编写的,使用此处找到的 node-phantom 库:https://github.com/alexscheelmeyer/node-phantom。虽然语法可能不同,但该库实际上直接与 phantomjs 一起使用,所以它不应该这样做有什么不同:
phantom.create(function (err,ph) {
ph.createPage(function (err,page) {
page.onResourceError = function(errorData) {
console.log('Unable to load resource (URL:' + errorData.url + ')');
console.log('Error code: ' + errorData.errorCode + '. Description: ' + errorData.errorString);
};
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
if(status==='success') {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', function () {
if(fetch_results) {
//THIS IS WHERE YOU WILL DO RESULTS SHIT
console.log("results page stuff entered");
page.render('phantomjs-test2.png');
ph.exit();
} else {
page.evaluate(function () {
//page evaluate stuff
}, function(err, result) {
console.log("entering here");
page.render('phantomjs-test.png');
if(!err) fetch_results = true;
});
}
});
} else {
console.log(
"Error opening url \"" + page.reason_url
+ "\": " + page.reason
);
console.log("Connection failed.");
ph.exit();
}
}
//page.open("https://www.google.com",function (err,status) {});
page.open("https://www.pavoterservices.state.pa.us/Pages/PollingPlaceInfo.aspx",function (err,status) {});
});
}, {parameters:{'ignore-ssl-errors':'yes'}});
因此,对于使用 google.com 的 page.open,页面加载成功。但是,在列出其他 url 时,它会返回以下错误:
Unable to load resource (URL:https://www.pavoterservices.state.pa.us/Pages/PollingPlaceInfo.aspx); Error code: 2. Description: connection closed; Error opening url "undefined": undefined
任何关于为什么 google 会加载但没有列出的 url 的帮助将不胜感激!
【问题讨论】:
-
可能是用户代理标头?
-
作为参考,有人专门试图阻止 phantom.js 客户端 stackoverflow.com/questions/20862728/…
标签: javascript node.js phantomjs