火狐浏览器设置为“使用系统代理设置”之后,已经可以正常访问Google。
但是以下代码:
import requests
url1 = "https://storage.googleapis.com/cvdf-datasets/mnist/train-images-idx3-ubyte.gz"
url2 = "https://www.baidu.com"
url3 = "https://www.google.com.hk/"
resp = requests.get(url3)
print(resp.text)
返回错误信息:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.google.com.hk', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f36978edd30>: Failed to establish a new connection: [Errno 101] Network is unreachable',))
原因应该是Python未通过代理直接访问网络,那应该如何设置Python系统默认代理访问网络呢?
如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求:
你也可以通过环境变量 HTTP_PROXY 和 HTTPS_PROXY 来配置代理。