使用多个代理限制请求

时间:2014-09-18 17:50:32

标签: python proxy web-scraping scrapy throttling

我目前正在通过自定义中间件为请求分配随机代理。我想将下载限制键下载到请求正在使用的特定代理,但据我所知,开箱即用,这只有在绑定到域或IP时才有可能。我担心在代理中间件中实现池逻辑会导致线程安全问题。有没有人这样做过?任何指针都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

As recommended on the Scrapy mailing list,有Autothrottle middleware obeys的特殊请求元变量,称为download_slot - 这允许对请求进行编程分组/限制。

在我的自定义代理中间件中:

self.proxies = get_proxies() #list of proxies
proxy_address = random.choice(self.proxies)
request.meta['proxy'] = proxy_address
request.meta['download_slot'] = hash(proxy_address) % MAX_CONCURRENT_REQUESTS

我使用哈希函数作为一种廉价的方式来通过外部定义的请求限制来处理请求。