進行評估我正在嘗試使用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