0

我开发了一个基于 Actor+PuppeteerCrawler+Proxy 的爬虫,并希望重新抓取失败的页面。为了增加重新刮擦的机会,我想切换到另一个 proxyUrl。这个想法是,使用修改后的 launchPupperteer 函数和不同的 proxyUrl 创建一个新的爬虫,并重新入队失败的页面。请检查下面的示例代码。

但不幸的是,它不起作用,尽管我通过使用 drop 和重新打开来重置请求队列。是否可以通过使用具有不同 proxyUrl 的 PuppeteerCrawler 来重新抓取失败的页面,以及如何?

最好的问候,沃尔夫冈

for(let retryCount = 0; retryCount <= MAX_RETRY_COUNT; retryCount++){

    if(retryCount){
        // Try to reset the request queue, so that failed request shell be rescraped
        await requestQueue.drop();
        requestQueue = await Apify.openRequestQueue();   // this is necessary to avoid exceptions
        // Re-enqueue failed urls in array failedUrls >>> ignored although using drop() and reopening request queue!!!
        for(let failedUrl of failedUrls){
            await requestQueue.addRequest({url: failedUrl});
        }
    }

    crawlerOptions.launchPuppeteerFunction = () => {
        return Apify.launchPuppeteer({
            // generates a new proxy url and adds it to a new launchPuppeteer function
            proxyUrl: createProxyUrl()
        });
    };

    let crawler = new Apify.PuppeteerCrawler(crawlerOptions);
    await crawler.run();

}
4

1 回答 1

1

我认为你的方法应该有效,但另一方面它不应该是必要的。我不确定是什么createProxyUrl

您可以提供一个带有用户名的通用代理 URL auto,它将使用您在 Apify 的所有数据中心代理。或者您可以proxyUrls直接提供给PuppeteerCrawler.

只是不要忘记您必须切换浏览器才能从代理获取新 IP。本文中的更多内容 - https://help.apify.com/en/articles/2190650-how-to-handle-blocked-requests-in-puppeteercrawler

于 2019-11-27T09:39:57.243 回答