【问题标题】:While using pandas got error urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed使用 pandas 时出现错误 urlopen 错误 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败
【发布时间】:2017-11-21 14:45:26
【问题描述】:

这是我在 python3 中的代码:

import pandas as pd

tables = pd.read_html("http://http.com/")

print(tables)

我得到了这个错误:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 401, in wrap_socket
    _context=self, _session=session)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 808, in __init__
    self.do_handshake()
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1061, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/pandas/io/html.py", line 906, in read_html
    keep_default_na=keep_default_na)
  File "/usr/local/lib/python3.6/site-packages/pandas/io/html.py", line 743, in _parse
    raise_with_traceback(retained)
  File "/usr/local/lib/python3.6/site-packages/pandas/compat/__init__.py", line 344, in raise_with_traceback
    raise exc.with_traceback(traceback)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>

到目前为止,这一直运行良好,但是我在这个特定的网站上遇到了这个问题。另外,我是这方面的新手,对 SSL 证书了解不多,我在 google 上搜索过,但对它仍然了解不多。

【问题讨论】:

    标签: python-3.x pandas ssl


    【解决方案1】:

    使用 -

    import ssl
    
    ssl._create_default_https_context = ssl._create_unverified_context
    

    【讨论】:

    • 这是什么黑魔法?它也解决了我的问题。
    • 这在 Windows 上对我有用,conda python 3.9.7。 Pandas read_csv() 在 url 上失败,上面修复了它。
    • 把错误改成HTTPError: HTTP Error 403: Errodeconfiguracaodobrowser
    • 如何使用 ssl 绕过验证?
    【解决方案2】:

    对于 MAC
    单击 Applications => Python,然后双击 Install Certificates.command。

    【讨论】:

    • 但是应用程序中没有 Python...你能说出应用程序中的确切位置吗?我假设 Finder => 应用程序。
    • 非常简单的解决方案,特别是在找到了这么多复杂的替代方案之后。非常感谢!
    • 对于学生来说,@markroxor 的回答很简单,但对于现实生活来说,Mayank 的回答是正确的方法
    • 这应该是经过验证的答案。谢谢!
    猜你喜欢
    • 2019-07-03
    • 2019-01-17
    • 1970-01-01
    • 2020-04-06
    • 2020-03-04
    • 2022-11-28
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多