urllib.request.urlopen(URL)在python3中不起作用
我正在尝试使用urllib.request.urlopen访问JSON。在python2中使用urllib2时效果很好,但不使用urllib.request.urlopenurllib.request.urlopen(URL)在python3中不起作用,python,python-3.x,urllib,urllib2,urlopen,Python,Python 3.x,Urllib,Urllib2,Urlopen,我正在尝试使用urllib.request.urlopen访问JSON。在python2中使用urllib2时效果很好,但不使用urllib.request.urlopen URL = 'https://api.exchangeratesapi.io/latest' f = urllib.request.urlopen(URL) 在处理上述异常期间,发生了另一个异常: Traceback (most recent call last): File "a1.py", line 19, in
URL = 'https://api.exchangeratesapi.io/latest'
f = urllib.request.urlopen(URL)
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last):
File "a1.py", line 19, in <module>
print(getLatestRates())
File "a1.py", line 15, in getLatestRates
f = urllib.request.urlopen(URL)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1359, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1318, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:864)>
您可以尝试禁用ssl验证
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
这就是我所用的,它起作用了:
from urllib.request import urlopen
URL = 'https://api.exchangeratesapi.io/latest'
f = urlopen(URL)
我希望这对你有用 在我的例子中,request.py中有一个错误问题: _加载\u windows\u存储\u证书导致内存错误 通过将python从3.7.4版更新到3.8.1版进行修复
希望获得此帮助您是在防火墙后面还是使用带有捕获屏幕的公共wifi?您是在windows还是mac上?您是否安装了Python3.7的根证书?也许你应该读一读:这可能是问题所在。
from urllib.request import urlopen
URL = 'https://api.exchangeratesapi.io/latest'
f = urlopen(URL)