我正在尝试运行一个爬虫,其输出日志如下所示:
2017-04-25 20:22:22 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <429 http://www.apkmirror.com/apk/instagram/instagram-instagram/instagram-instagram-9-0-0-34920-release/instagram-9-0-0-4-android-apk-download/>: HTTP status code is not handled or not allowed
2017-04-25 20:22:22 [scrapy.core.engine] INFO: Closing spider (finished)
2017-04-25 20:22:22 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 16048410,
'downloader/request_count': 32902,
'downloader/request_method_count/GET': 32902,
'downloader/response_bytes': 117633316,
'downloader/response_count': 32902,
'downloader/response_status_count/200': 121,
'downloader/response_status_count/429': 32781,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 4, 25, 18, 22, 22, 710446),
'log_count/DEBUG': 32903,
'log_count/INFO': 32815,
'request_depth_max': 2,
'response_received_count': 32902,
'scheduler/dequeued': 32902,
'scheduler/dequeued/memory': 32902,
'scheduler/enqueued': 32902,
'scheduler/enqueued/memory': 32902,
'start_time': datetime.datetime(2017, 4, 25, 17, 54, 36, 621481)}
2017-04-25 20:22:22 [scrapy.core.engine] INFO: Spider closed (finished)
简而言之,在32,902个请求中,只有121个成功(响应代码200),而其余的则收到429个“太多请求”(参见https://httpstatuses.com/429)。
有什么推荐的方法可以解决这个问题吗?首先,我想看看
429
响应的详细信息,而不仅仅是忽略它,因为它可能包含Retry-After header ,指示在发出新请求之前要等待多长时间。同样,如果按照http://blog.michaelyin.info/2014/02/19/scrapy-socket-proxy/中所述使用Privoxy和Tor发出请求,则可能会实现重试中间件,该中间件会使Tor在发生这种情况时更改其IP地址。是否有此类代码的公开示例?
最佳答案
哇,您的抓取工具的运行速度非常快,在30分钟内超过30,000个请求。每秒超过10个请求。
如此高的交易量将触发较大站点上的速率限制,并将完全关闭较小站点。 不要这样做。
同样,对于privoxy和tor来说,这可能甚至太快了,因此,这些也可能是429答复的候选者。
解决方案:
关于web-scraping - 如何在Scrapy中处理429个请求过多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43630434/