如何在scrapy中一起使用http和https代理?

时间:2015-07-09 09:41:00

标签: python scrapy

我是scrapy的新手。我发现使用http代理但我想一起使用http和https代理,因为当我抓取链接时,有http和https链接。我如何使用http和https代理?

class ProxyMiddleware(object):
    def process_request(self, request, spider):
        request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"
        #like here request.meta['proxy'] = "https://YOUR_PROXY_IP:PORT"
        proxy_user_pass = "USERNAME:PASSWORD"
        # setup basic authentication for the proxy
        encoded_user_pass = base64.encodestring(proxy_user_pass)
        request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass

1 个答案:

答案 0 :(得分:4)

您可以将标准环境变量与HttpProxyMiddleware

组合使用
  

此中间件通过为Request对象设置代理元值来设置用于请求的HTTP代理。

     

与Python标准库模块urllib和urllib2一样,它遵循以下环境变量:

http_proxy
https_proxy
no_proxy
     

您还可以将每个请求的元键代理设置为http://some_proxy_server:port

之类的值