middewares里:
class ProxyMiddleWare(object):
def process_request(self, request, spider):
proxy = random.choice(PROXIES)
- if proxy['user_passwd'] is None:
- # if 'user_passwd' not in proxy:
- # 没有代理账户验证的代理使用方式
- print('---------------------->>> ', proxy['ip_port'])
- request.meta['proxy'] = "http://" + proxy['ip_port']
- # request.meta['proxy'] = 'http://122.235.168.162:8118'
- else:
- # 对账户密码进行base64编码转换
- base64_userpasswd = base64.b64encode(proxy['user_passwd'].encode())
- # 对应到代理服务器的信令格式里
- request.headers['Proxy-Authorization'] = 'Basic ' + base64_userpasswd.decode()
- request.meta['proxy'] = "http://" + proxy['ip_port']
setting里:
PROXIES = [
# {'ip_port': '61.175.192.2:35420'},
# {'ip_port': '221.234.192.10:8010'},
{'ip_port': '221.224.49.194:51127', 'user_passwd': ''},
# {"ip_port": "121.41.8.23:16816", "user_passwd": "morganna_mode_g:ggc22qxp"},
# {'ip_port': '122.224.249.122:8088', 'user_passwd': 'user4:pass4'},
]
DOWNLOADER_MIDDLEWARES = {
'taobao.middlewares.TaobaoDownloaderMiddleware': 543,
'taobao.middlewares.SeleniumMiddleware': 543,
'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 751,
- 'taobao.middlewares.ProxyMiddleWare': 750,
- 'taobao.middlewares.RandomUserAgent': 400,
}
我是这么设置的,而且这个IP用request测试过,返回状态码也是200,但在scrapy里不能正常抓取数据,请教各位大神指点下