4
Subj。我們的蜘蛛遵循鏈接並用「解析頁面」函數解析它們,該函數返回項目。在第一次調用parse_page之前,如何爲每個請求添加不同的代理?如何爲每個請求(或線程)添加不同的代理到scrapy
例如,我有250個代理池,並且想要隨機選擇每個請求。
Subj。我們的蜘蛛遵循鏈接並用「解析頁面」函數解析它們,該函數返回項目。在第一次調用parse_page之前,如何爲每個請求添加不同的代理?如何爲每個請求(或線程)添加不同的代理到scrapy
例如,我有250個代理池,並且想要隨機選擇每個請求。
您可以爲此創建一些中間件。例如:
#Start your middleware class
class ProxyMiddleware(object):
# overwrite process request
def process_request(self, request, spider):
# Set the location of the proxy
request.meta['proxy'] = "http://123.456.789.012"
# Use the following lines if your proxy requires authentication
proxy_user_pass = "USER_AND_PASS"
# setup basic authentication for the proxy
encoded_user_pass = base64.encodestring(proxy_user_pass)
request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
我相信,你可以很容易地通過修改上面的代碼中隨機代理網址,用戶名和密碼。如果您需要任何其他幫助,請告知我。