python3下scarpy爬虫代理错误407

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 改的,感觉是编码问题,但是不知道怎么改,求大神帮忙看看是啥问题

阅读 6.7k
2 个回答

低级错误,发现是'Basic'这个缺少一个空格,
但是不知为啥 'Basic' +' '+str(encoded_user_pass,encoding='utf-8')也是不行的。

代理未设置正确。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题