在middlewares.py文件里添加代理ip

import  random
import  base64

#方法一
#设置代理ip
class    IpProxyDownLoadMiddleWares(object):
    Proxys=['178.44.170.152:8080','110.44.113.182:8080','209.126.124.73:8080']
    
    def  process_request(self,request,spider):
        proxy=random.choice(self.Proxys)
        request.meta['proxy']=proxy


#方法二
#连接到其他服务器
class   IpProxyDownLoadMiddleWares(object):
    def  process_request(self,request,spider):
        proxy='121.199.6.124:16816'
        user_password='970138074:rcdj35ur'

        b64_user_password=base64.b64encode(user_password.encode('utf-8'))
        request.headers['Proxy-Authrization']="Basic"+b64_user_password.decode('utf-8')

注意:最后还要在setting文件里面开启middlewares中间键

你可能感兴趣的:(在middlewares.py文件里添加代理ip)