Python 3

在使用urllib.request.urlopen(url)时出现

报错代码如下:

urllib.error.HTTPError: HTTP Error 407: Proxy Authentication Required

解决办法:需要给urllib配置代理

import urllib.request as req

proxy = req.ProxyHandler({'http': r'http://username:password@url:port'})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
conn = req.urlopen('https://www.baidu.com')
res = conn.read()
print(res)

参考文档:https://qa.1r1g.com/sf/ask/823411991/

原文地址:https://www.cnblogs.com/hello-python2020/p/13932916.html