首页 > 解决方案 > 无法访问 python 中的 Twitter 包及其功能

问题描述

我已经twitter在 python 中安装并导入了这个包。在初始化 Twitter 类实例之后,因为t这是我尝试过的。

>>> t.statuses.home_timeline
<twitter.api.TwitterCall object at 0x10646e160>
>>> print(t.statuses.home_timeline)
<twitter.api.TwitterCall object at 0x106d1f2e0>
>>> t.statuses.home_timeline()

最后一行返回以下内容 -


Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1319, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1276, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1225, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1004, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 944, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1399, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/twitter/api.py", line 334, in __call__
    return self._handle_response(req, uri, arg_data, _timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/twitter/api.py", line 341, in _handle_response
    handle = urllib_request.urlopen(req, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1362, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1322, 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:1108)>

我无法解决这个问题。此外,我尝试搜索类似的问题,但如果它已经存在,我无法得到它,我为麻烦道歉。

配置 - MacOS Catalina 10.15.4 Python3.8出现错误,Python2.7没有这个问题(我机器上的默认python)

注意:已尝试使用pip install certifipython3 版本,pip 给我一条消息,说“要求已经满足”。还检查了 python3 中的文件夹,并找到了一个名为的目录etc,其中包含一个文件夹openssl。OpenSSL 文件夹本身是空的。

标签: pythontwitter

解决方案


要解决此问题,请运行命令:

pip install certifi

编辑:对于 Mac OS 用户,进入 Applications 文件夹并展开 Python 文件夹。现在首先运行(或双击)Install Certificates.command,然后运行 ​​Update Shell Profile.command


推荐阅读