我正在尝试将 NightmareJS v2 和 Vo 与 Node.JS 一起使用来浏览一些搜索词并从中收集数据。我的代码如下所示:
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
. 当我尝试这个时,我变得不确定。我认为它与发电机有关,但我对它们很陌生,所以我不能说。帮助表示赞赏。