当前位置: 首页 > 知识库问答 >
问题:

urllib.request.urlopen(URL)不工作在python3

胥良平
2023-03-14

我正在尝试使用urllib访问JSON。要求urlopen。在python2中使用urllib2时效果很好,但不是urllib。要求urlopen。

URL = 'https://api.exchangeratesapi.io/latest'

f = urllib.request.urlopen(URL)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1316, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1391, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 415, in wrap_socket
    _session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 826, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1080, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 701, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:864)

在处理上述异常时,发生了另一个异常:

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)>

共有3个答案

宁卓
2023-03-14

在我的情况下,有一个错误的request.py:

_load_windows_store_certs造成内存错误

通过更新python从版本3.7.4到3.8.1修复

希望这有帮助

汤枫
2023-03-14

您可以尝试禁用ssl验证

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 
诸葛苏燕
2023-03-14

这是我用的,它起作用了:

from urllib.request import urlopen

URL = 'https://api.exchangeratesapi.io/latest'
f = urlopen(URL)

我希望这对你有用!

 类似资料:
  • 问题内容: 我一直在玩漂亮的汤和解析网页几天。在编写的所有脚本中,我一直使用一行代码作为救星。代码行是: 但是… 我想对(OPEN A URL WITH AUTHENTICATION)做同样的事情: 我无法打开网址并阅读需要身份验证的网页。我如何实现这样的目标: 问题答案: 看看官方文档中的如何使用urllib软件包获取Internet资源:

  • 我正在使用以下web.xml将WAR文件(vimbaserver-1.0.war)部署到我的JBoss Web服务器: WAR包含我要测试的两个REST函数: 综上所述,我们希望以下REST URL能够正常工作:http://xx.xx.xx.xx:8080/vimbaserver-1.0/functions/getWord/test。然而,情况似乎并非如此。 奇怪的是,我们在服务器上部署了另一个

  • 我正在从事一个使用SpringDataREST和JPA的项目,我正在尝试配置一个HTTP拦截器。根据Spring Web MVC docs-Handler Mapping Interceptor中提供的参考文档,我创建了一个扩展HandlerInterceptorAdapter的组件,如下所示: 然后,通过扩展WebMvcConfig(如Spring Web MVC Docs-Config Int

  • 下面的URL模块正在工作 但无法在服务器上像这样运行它: 为什么?? 我怎样才能像localhost?一样在服务器上正常运行 我使用密码点火器。

  • 我有一个svg,它有两条路径,一条对角线和一个小圆。它们的笔画颜色引用了defs中的linearGradient。在Chrome,Firefox和Safari上,小圆圈呈现。但是在Edge和IE 11上,小圆圈并没有将url路径连接到线性渐变的id上,如果我将它的stroke属性更改为颜色,那么它就会呈现,但是我想使用url值。 这里也有代码接口链接

  • 在Android9的coroutines中使用URL时,我的游戏不起作用。对于较低级别的API来说,它工作得很好。这是我的代码: