我正在用Nightmare抓取一个网页,想知道如何在输入数组上重用一个函数。
假设我有一个检索页面标题的方法
function* test(url,callback) {
var size = { width: 1920, height: 1080, 'use-content-size': true, show: true }
var nightmare = Nightmare(size)
var title = yield nightmare
.goto('http://cnn.com')
.evaluate(function () {
return document.title
});
console.log(title)
yield nightmare.end()
callback()
}我想在urls数组上执行此方法。因此,我使用异步库来遍历整个数组,并对urls的urls数组执行test函数。
async.each(urls, test, function (err) {
console.log('done!');
});但是async.each不支持生成器函数,如何将测试函数更改为普通函数而不是生成器函数。
发布于 2016-01-11 03:06:12
我已经找到了一种方法-我需要使用promises库来执行多个函数,这是一个普通的函数。
function test(url){
Promise.resolve( // call for the promises library with the nightmare instance
nightmare
.goto() //all the calls that you need
.wait()
.....
).then(function (result) { //This function will be called when the block of commands is done , with the result.
console.log("Done")
}, function (err) { //Error handling
console.log(err);
});
}https://stackoverflow.com/questions/33085985
复制相似问题