繁体   English   中英

使用 puppeteer 集群的无限循环(故意)

[英]Infinite loop (on purpose) using puppeteer cluster

我对 puppeteer-cluster 很陌生。 我的目标是无限地抓取 100 个站点的列表,所以一旦我到达第 100 个链接,脚本就会重新开始(理想情况下重用同一个集群实例)。 有没有更好的方法或正确的方法来做到这一点? 我在想故意有一个无限循环(和旋转元素)可能会更容易。 任何意见,将不胜感激。

这是我的代码:

(async () => {
    const cluster = await Cluster.launch({
        concurrency: Cluster.CONCURRENCY_CONTEXT,
        maxConcurrency: 20,
        monitor: true
    });

    // Extracts document.title of the crawled pages
    await cluster.task(async ({ page, data: url }) => {
        await page.goto(url, { waitUntil: 'domcontentloaded' });
        const pageTitle = await page.evaluate(() => document.title);
        console.log(pageTitle);
    });

    // In case of problems, log them
    cluster.on('taskerror', (err, data) => {
        console.log(`  Error crawling ${data}: ${err.message}`);
    });

    while (true) {
        await new Promise(resolve => setTimeout(crawl, 5000));
    }

    async function crawl() {
        for (let i = 0; i < sites.length; i++) {
            const site = sites[i];

            site["product_urls"].forEach(async (url) => {
                await cluster.execute(url);
            });
        }

        await cluster.idle();
    }
})();
for (;;) {}

会给你一个无限循环,而不会遇到诸如 ESLint 之类的任何问题,或者关于“无法访问的代码”的警告。

也就是说,设置回退条件可能不会有什么坏处,以便您的代码能够在需要时安全结束

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM