Javascript 如何在超过超时时阻止NodeJS脚本崩溃

Javascript 如何在超过超时时阻止NodeJS脚本崩溃,javascript,node.js,timeout,settimeout,puppeteer,Javascript,Node.js,Timeout,Settimeout,Puppeteer,下面您可以找到发生的错误 我正在尝试使用NodeJS和puppeter来抓取网站的内容。有时,代码会在超过错误超时时停止。是否有一种方法可以让我在超过页面加载超时的情况下运行一个函数,该函数可以重新加载页面,或者让脚本等待几秒钟,然后重新加载页面,直到它正确获取数据,而不会崩溃? 如果是这样,我将如何实施它 多谢各位 (node:8300) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 3

下面您可以找到发生的错误

我正在尝试使用NodeJS和puppeter来抓取网站的内容。有时,代码会在超过错误超时时停止。是否有一种方法可以让我在超过页面加载超时的情况下运行一个函数,该函数可以重新加载页面,或者让脚本等待几秒钟,然后重新加载页面,直到它正确获取数据,而不会崩溃? 如果是这样,我将如何实施它

多谢各位

(node:8300) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\Users\danie\node_modules\puppeteer\lib\LifecycleWatcher.js:143:21)
  -- ASYNC --
    at Frame.<anonymous> (C:\Users\danie\node_modules\puppeteer\lib\helper.js:108:27)
    at Page.goto (C:\Users\danie\node_modules\puppeteer\lib\Page.js:656:49)
    at Page.<anonymous> (C:\Users\danie\node_modules\puppeteer\lib\helper.js:109:23)
    at scrape (C:\Users\danie\Documents\Node Projects\p-download.js:23:14)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:8300) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8300) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(节点:8300)未处理PromisejectionWarning:TimeoutError:超出导航超时:超出30000ms
在Promise.then(C:\Users\danie\node\u modules\puppeter\lib\LifecycleWatcher.js:143:21)
--异步的--
在画面上。(C:\Users\danie\node\u modules\puppeter\lib\helper.js:108:27)
在Page.goto(C:\Users\danie\node\u modules\puppeter\lib\Page.js:656:49)
在第页。(C:\Users\danie\node\u modules\puppeter\lib\helper.js:109:23)
在scrap(C:\Users\danie\Documents\Node Projects\p-download.js:23:14)
在进程中。_tick回调(内部/process/next_tick.js:68:7)
(节点:8300)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:1)
(节点:8300)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
我的代码:

const puppeteer = require('puppeteer');

let scrape = async () => {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();

    await page.setRequestInterception(true);    
    page.on('request', (req) => {
        if(req.resourceType() == 'stylesheet' || req.resourceType() == 'script' || req.resourceType() == 'font' || req.resourceType() == 'media' || req.resourceType() == 'image'){
            req.abort();
        }
        else {
            req.continue();
        }
    }); //Disables loading CSS, images and scripts

    for(i=0; i<5000; i++){
        await page.goto('https://website.com/' + i);
        let result = await page.evaluate(() => {
            var result = '';
            for (i=1; i<=10; i++){
                result += document.getElementsByTagName('td')[i].innerText;
                result += ',';
            }
            result += '\n';
            return result;
        });
    }
}
scrape();
const puppeter=require('puppeter');
让scrape=async()=>{
const browser=wait puppeter.launch({headless:false});
const page=wait browser.newPage();
等待页面。setRequestInterception(true);
第页('请求',(请求)=>{
如果(req.resourceType()=='stylesheet'| | req.resourceType()=='script'| | req.resourceType()=='font'| | req.resourceType()=='image'){
请求中止();
}
否则{
请求继续();
}
});//禁用加载CSS、图像和脚本
对于(i=0;i{
var结果=“”;

对于(i=1;i将代码放入try/catch块以避免崩溃…我会将循环中的代码移到另一个函数中

for(i=0; i<5000; i++){
    result = await open_page(page , i );
}


async function open_page(page , i ){
    try {
            await page.goto('https://website.com/' + i);
            let result = await page.evaluate(() => {
                var result = '';
                for (i=1; i<=10; i++){
                    result += document.getElementsByTagName('td')[i].innerText;
                    result += ',';
                }
                result += '\n';
                return result;
            });


            return {stat:1 , result : result } ;

    }
    catch(e){
        return {stat:0 , error : e } ;
    }

}
(i=0;i)的
{
var结果=“”;

因为(i=1;我感谢你。在抓(e)之后相反,我为错误添加了console.log,然后我--再次重复这个过程。@DanielRika你知道,当你编写一个机器人来删除数据时,你必须避免被卡在一个链接上……比如如果
site.com/100
由于某种原因没有打开,你会一遍又一遍地调用这个地址,而其他号码永远不会被调用,你必须是的,我理解,但在这种情况下,每个链接都是功能性的,到目前为止,它工作得非常好。唯一的问题是,它有时相当慢。不过,感谢您的帮助:)@DanielRika您可以同时在多个选项卡上打开多个链接,这很有趣。您可以参考任何代码或脚本,让我了解它的工作原理吗?