我正在尝试使用 Node-horseman 编写一个基本的网络抓取工具,但是我在 evaluate 函数中遇到了一些 CSS 选择器问题。对于某些网站,evaluate 功能工作得很好,但对于其他网站,Node 会抛出错误。
例如:
var Horseman = require('node-horseman');
var horseman = new Horseman();
function getTitle() {
var title = $('.product-name-main h1').text();
return {title: title};
}
horseman
.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0')
.open('http://shtoraoptom.ru/pokryvalo-nazsu-gul-abrikosovyj-240-260-sm')
.evaluate(getTitle)
.then(function(title){
console.log(title);
return horseman.close();
});
此代码引发错误:
Unhandled rejection getTitle evaluate
global code evaluateJavaScript@[native code] evaluate@phantomjs://platform/webpage.js:390:39 phantomjs://code/bridge.js:121:61 at Horseman.<anonymous> (/home/ubuntu/workspace/node_modules/node-horseman/lib/actions.js:839:38)
at Horseman.tryCatcher (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/promise.js:502:31)
at Promise._settlePromise (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/promise.js:559:18)
at Promise._settlePromiseCtx (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/promise.js:596:10)
at Async._drainQueue (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/async.js:143:12)
at Async._drainQueues (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/async.js:148:10)
at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/workspace/node_modules/node-horseman/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate [as _immediateCallback] (timers.js:383:17)
当我尝试使用相同的选择器以另一种方式重写代码时,它会正确地输出标题:
var Horseman = require('node-horseman');
var horseman = new Horseman();
function getTitle() {
var title = $('.product-name-main h1').text();
return {title: title};
}
horseman
.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0')
.open('http://shtoraoptom.ru/pokryvalo-nazsu-gul-abrikosovyj-240-260-sm')
.text('.product-name-main h1')
.then(function(title){
console.log(title);
return horseman.close();
});
出现此类错误的原因是什么?
最佳答案
原因
.evaluate()
方法期望客户端 JavaScript 函数在您正在抓取的网页中可用。
您定义的方式getTitle()
第一个示例中的函数意味着它是在 NodeJS 范围内创建的,而不是在抓取的网页客户端范围内创建的。
可能的修复
OPTION 1
您可以摆脱创建 getTitle()
函数,但将客户端 JavaScript 代码编写为 .evaluate()
的参数方法,像这样:
.evaluate(function () { return $('.product-name h1').first().text(); })
(要获得完整的解决方案,请查看我的 GitHub 存储库: https://github.com/omnimind/stackOverflow/blob/master/NodeJS/Controllers/37793821.js 名为 main()
的脚本方法)
OPTION 2
如果您坚持创建getTitle()
JavaScript 函数,那么您需要在单独的 JavaScript 文件中创建该函数,并将其注入(inject)到您使用 horsemen .injectJs(file)
抓取的网页中。像这样:
.injectJs('<external_javascript_file>').evaluate(function () { return getTitle(); })
(要获得完整的解决方案,请查看我的 GitHub 存储库: https://github.com/omnimind/stackOverflow/blob/master/NodeJS/Controllers/37793821.js 名为 external()
的脚本方法)
备注
第二个选项可以让您更好地组织代码,特别是在您需要注入(inject)的客户端 JavaScript 代码因其大小而变得更加复杂和健壮的情况下。
关于node.js - Node-horseman 未处理的拒绝评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37793821/