[Python] python scrapy 网络采集使用代理的方法 →→→→→进入此内容的聊天室

来自 , 2020-10-23, 写在 Python, 查看 144 次.
URL http://www.code666.cn/view/90248d0a
  1.  
  2. # Importing base64 library because we'll need it ONLY in case if the proxy we are going to use requires authentication
  3. import base64
  4.  
  5. # Start your middleware class
  6. class ProxyMiddleware(object):
  7.     # overwrite process request
  8.     def process_request(self, request, spider):
  9.         # Set the location of the proxy
  10.         request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"
  11.  
  12.         # Use the following lines if your proxy requires authentication
  13.         proxy_user_pass = "USERNAME:PASSWORD"
  14.         # setup basic authentication for the proxy
  15.         encoded_user_pass = base64.encodestring(proxy_user_pass)
  16.         request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
  17.  
  18.  
  19. #//python/8309

回复 "python scrapy 网络采集使用代理的方法"

这儿你可以回复上面这条便签

captcha