一.设置随机请求头 class UAMiddleWare(object): UA_LIST = [ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Fire
1.在settings文件中添加ip池 IPPOOL=['xxx.xx.xx.xx','xxx.xx.xxx.xx'] 2.在middleware文件中添加自己的代理ip类(首先需要导入ipPOOL,random模块) class Myproxymiddleware(object): def __init__(self,ip=''): self.ip = ip def process_request(self,request,spider) ip = random.choice(IPPOOL)