鍍金池/ 問答/Python/ python3下scarpy爬蟲代理錯誤407

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 改的,感覺是編碼問題,但是不知道怎么改,求大神幫忙看看是啥問題

回答
編輯回答
不歸路

代理未設置正確。

2017年6月25日 06:23
編輯回答
故林

低級錯誤,發(fā)現(xiàn)是'Basic'這個缺少一個空格,
但是不知為啥 'Basic' +' '+str(encoded_user_pass,encoding='utf-8')也是不行的。

2017年4月12日 16:25