本文介绍了Python的3 - 的urllib,HTTP错误407:需要代理身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图打开一个网站(我后面的企业代理)使用urllib.request.urlopen(),但我得到的错误:
urllib.error.HTTPError:HTTP错误407:需要代理身份验证我可以找到urllib.request.getproxies()代理,但如何指定用户名和密码才能使用呢?我找不到在官方文档解决方案。
解决方案 导入urllib.request里的REQ代理= req.ProxyHandler({'HTTP':r'http://用户名:密码@网址:口'})AUTH = req.HTTPBasicAuthHandler()首战= req.build_opener(代理,身份验证,req.HTTPHandler)req.install_opener(揭幕战)康恩= req.urlopen('google)return_str = conn.read()I'm trying to open a website (I am behind a corporate proxy) using urllib.request.urlopen() but I am getting the error:
urllib.error.HTTPError: HTTP Error 407: Proxy Authentication RequiredI can find the proxy in urllib.request.getproxies(), but how do I specify a username and password to use for it? I couldn't find the solution in the official docs.
解决方案 import urllib.request as req proxy = req.ProxyHandler({'http': r'username:password@url:port'}) auth = req.HTTPBasicAuthHandler() opener = req.build_opener(proxy, auth, req.HTTPHandler) req.install_opener(opener) conn = req.urlopen('google') return_str = conn.read()
更多推荐
Python的3
发布评论