def process_request(self,request,spider):
# 获取redis中所有的键(假设redis中只保存了cookies)
redisKeys = self.rconn.keys()
elem = random.choice(redisKeys)
request.cookies = cookie
# 在请求中记录当前cookies对应的账号和密码
request.meta["accountText"] = elem.split(":")[-1]
def process_response(self,request,response,spider):
if response.status in [300, 301, 302, 303]:
# 获取重定向的url
redirect_url = response.headers["location"]
if url == "login_url":# 如果是登陆页面,说明当前cookies失效了,需要更新
username,passworod = request.meta['accountText'].split("--")
update_cookie(spider_name,username,password)
elif url=="验证页面":# 说明账号被封了
username,passworod = request.meta['accountText'].split("--")
remove_cookie(spider_name,username,password)
request = request.copy()
request.dont_filter = True
return request