簡體   English   中英

如何為每個請求(或線程)添加不同的代理到scrapy

[英]How can i add different proxy to scrapy for every request(or thread)

SUBJ。 我們的蜘蛛跟隨鏈接並使用“解析頁面”功能解析它們,該函數返回項目。 在第一次調用parse_page之前,如何為每個請求添加不同的代理?

例如,我有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

我相信您可以通過修改上述代碼輕松隨機化代理URL,用戶名和密碼。 如果您需要任何其他幫助,請與我們聯系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM