Nodejs/puppeteer - 导航超时错误
Nodejs/puppeteer - Navigation timeout error
出现这样的错误后,我可以恢复连接吗
UnhandledPromiseRejectionWarning:
TimeoutError: Navigation Timeout
Exceeded: 1000ms exceeded
示例:
let arg = [] //array with urls
await page.goto(...args, {waitUntil: 'load', timeout: 1000 });
或者唯一的出路是设置超时?
我认为您的问题来自您为 puppeteer
的 goto
方法提供的参数:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options
当您调用 goto
时,它需要 string
url 而不是 Array<string>
.
回答原题:
不可以,page.goto()
函数超时后无法恢复连接。您只能处理异常并重试。
另一方面,如果您要完成的是加载页面,
我建议对您的代码进行两处更改:
第一个:
page.goto()
不接受 Array
或 Object
作为第一个参数,它需要是单数字符串,例如:
page.goto('https://www.google.com')
.
第二个:
除非您加载的页面非常简单,否则 1000 毫秒的 timeout
太短了。 Puppeteer 的默认值为 30000 毫秒,因此我建议使用它或设置至少 5000 毫秒的超时:
page.goto('https://www.google.com', { timeout: 5000 })
也没有必要使用 { waitUntil: 'load' }
,因为这是默认值。
希望这对您有所帮助。
如果你想对 args
数组中的所有 url 进行请求而不停止循环,如果一个将失败。
所以这是解决方案:
const async = require('async'); // npm i --save async
const urls = [... array of urls ...];
const execution = {
total: urls.length,
success: 0,
failed: 0,
results: []
};
async.eachLimit(
urls,
10,
async (url, done) => {
try {
const data = await page.goto(url, {waitUntil: 'load', timeout: 1000});
execution.success++;
execution.results.push({url, data});
}
catch (error) {
execution.failed++;
execution.results.push({url, data: null, error: error.message});
}
finally {
done();
}
},
(errors) => {
console.log('Finished:', execution);
});
出现这样的错误后,我可以恢复连接吗
UnhandledPromiseRejectionWarning:
TimeoutError: Navigation Timeout
Exceeded: 1000ms exceeded
示例:
let arg = [] //array with urls
await page.goto(...args, {waitUntil: 'load', timeout: 1000 });
或者唯一的出路是设置超时?
我认为您的问题来自您为 puppeteer
的 goto
方法提供的参数:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options
当您调用 goto
时,它需要 string
url 而不是 Array<string>
.
回答原题:
不可以,page.goto()
函数超时后无法恢复连接。您只能处理异常并重试。
另一方面,如果您要完成的是加载页面,
我建议对您的代码进行两处更改:
第一个:
page.goto()
不接受 Array
或 Object
作为第一个参数,它需要是单数字符串,例如:
page.goto('https://www.google.com')
.
第二个:
除非您加载的页面非常简单,否则 1000 毫秒的 timeout
太短了。 Puppeteer 的默认值为 30000 毫秒,因此我建议使用它或设置至少 5000 毫秒的超时:
page.goto('https://www.google.com', { timeout: 5000 })
也没有必要使用 { waitUntil: 'load' }
,因为这是默认值。
希望这对您有所帮助。
如果你想对 args
数组中的所有 url 进行请求而不停止循环,如果一个将失败。
所以这是解决方案:
const async = require('async'); // npm i --save async
const urls = [... array of urls ...];
const execution = {
total: urls.length,
success: 0,
failed: 0,
results: []
};
async.eachLimit(
urls,
10,
async (url, done) => {
try {
const data = await page.goto(url, {waitUntil: 'load', timeout: 1000});
execution.success++;
execution.results.push({url, data});
}
catch (error) {
execution.failed++;
execution.results.push({url, data: null, error: error.message});
}
finally {
done();
}
},
(errors) => {
console.log('Finished:', execution);
});