gpt4 book ai didi

javascript - 在 nightmare.js 中循环 url 时的异步挑战

转载 作者:行者123 更新时间:2023-11-29 10:09:59 25 4
gpt4 key购买 nike

我正在使用异步模块通过 nightmarejs 迭代多个 url。我无法创建新的 Nightmare 实例,因为我每次都必须重新进行身份验证。

所以我正在尝试使用异步模块。我遇到了一个(我认为是经典的)问题,所有迭代的 url 都是数组中的最终 url——而不是每个单独的 url。我以为使用 async 模块可以解决这个问题(我也尝试过使用 let),但我仍然遇到问题

'use strict'

var Nightmare = require("nightmare");
var async = require("async");

//Creates the authenticated nightmare instance

var scraper = new Nightmare()
.goto('https://www.example.com/signin')
.type('#login', 'username')
.type('#password', 'password')
.click('#btn')
.run(function(err, nightmare) {
if (err) {
console.log(err);
}
console.log('Done.');
});

//Trying to use async module to iterate through urls

function load(url, callback){
scraper
.goto(url)
.wait(2000)
.screenshot('pic'+url[25]+'.png')
.run(function(err, nightmare) {
if (err) {
console.log(err);
}
console.log('Done with ', url[25]);
callback()
});
}

var urls = [
'https://www.example.com/p1',
'https://www.example.com/p2',
'https://www.example.com/p3',
]

async.each(urls, load, function (err) {
console.log('done!');
});

谢谢你的建议

最佳答案

问题在于这一行:

async.each(urls, load, function (err) {

默认情况下,async 并行运行 each(see the documentation here,如果您好奇的话)。 Nightmare 无法并行执行多个请求,因此,这样做会导致如您所见的错误结果。

解决方案很简单:改用 async.eachSeries。这将保证您的请求将连续运行,从而使 Nightmare 能够按设计运行。

关于javascript - 在 nightmare.js 中循环 url 时的异步挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35109367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com