class ProxyMiddleware(object):
def process_request(self, request, spider):
request.meta['proxy'] = 'http://myproxy.com:8080'
proxy_user_pass = "username:password"
encoded_user_pass = base64.b64encode(proxy_user_pass.encode("utf-8"))
request.headers['Proxy-Authorization'] = 'Basic' + str(encoded_user_pass,encoding='utf-8')
python3 下用 scrapy 爬虫,因为要用公司的代理,所以在中间件里设置了代理,目前测试代理验证失败。返回 407 错误代码。
2017-11-13 11:04:32 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <407 http://www.sqsxs.com/sort2/1.html>: HTTP status code is not handled or not allowed
代码是按 python2 改的,感觉是编码问题,但是不知道怎么改,求大神帮忙看看是啥问题
低级错误,发现是'Basic'这个缺少一个空格,
但是不知为啥 'Basic' +' '+str(encoded_user_pass,encoding='utf-8')也是不行的。