使用新的代理 URL 重试失败的页面



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

但不幸的是,它不起作用,尽管我通过使用删除和重新打开来重置请求队列。是否可以通过使用具有不同代理URL的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();
}

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

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

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

最新更新