进行评估我正在尝试使用NightmareJS v2和Vo以及Node.JS来查看几个搜索项并从中收集数据。我的代码可以看到下面:NightmareJS无法使用vo
const nightmare = require('nightmare'),
vo = require('vo'),
nbot = nightmare({ title: 'Bot',
show: true });
const searchTerms = ['spacex', 'tesla', 'elon musk', 'hyperloop']; // EXAMPLE SEARCH TERMS
vo(run)(function(err) {
if (err) throw err
});
function * run() {
yield nbot.goto('http://google.com');
yield * forEach(searchTerms, gen);
yield nbot.end()
.then(function(result) {
console.log(result) // STUFF SHOULD BE LOGGED HERE
});
}
function * gen(item) {
yield nbot.wait('input[title="Search"]')
.click('input[title="Search"]')
.type('input[title="Search"]', item)
.click('input[name="btnK"]')
.wait(100)
.screenshot(item + '.png')
.insert('input[title="Search"]', '')
.evaluate(function() {
return 'foobar' // STUFF RETURNED HERE
})
}
function * forEach (arr, fn) { // NEEDED BECAUSE FOREACH DOESN'T WORK IN GENERATORS
let i;
for (i = 0; i < arr.length; i++) {
yield * fn(arr[i]);
}
}
根据NightmareJS的文档,如果返回的evaluate
里面的东西,然后它吐了出来,当您使用then
。当我尝试这个时,我得到了未定义。我认为这是发电机的东西,但我是他们的新手,所以我不能说。帮助表示赞赏。
谢谢!但是,我在昨天解决了一个小时左右后,才真正解决了这个问题。我会将你的答案标记为在未来帮助其他人解决这个问题。 –
太棒了。真高兴你做到了。 :) – Ross