2016-04-05 56 views
1
import requests 

spiders = requests.get('https://en.wikipedia.org/w/api.php') 
print(spiders) 

有了上面的代碼我收到以下錯誤:無法訪問維基百科API由於SSL證書錯誤

Traceback (most recent call last): 
    File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 554, in urlopen 
    self._prepare_proxy(conn) 
    File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 750, in _prepare_proxy 
    conn.connect() 
    File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 252, in connect 
    ssl_version=resolved_ssl_version) 
    File "C:\Python\lib\site-packages\requests\packages\urllib3\util\ssl_.py", line 305, in ssl_wrap_socket 
    return context.wrap_socket(sock, server_hostname=server_hostname) 
    File "C:\Python\lib\ssl.py", line 376, in wrap_socket 
    _context=self) 

    File "C:\Python\lib\ssl.py", line 747, in __init__ 
    self.do_handshake() 
    File "C:\Python\lib\ssl.py", line 983, in do_handshake 
    self._sslobj.do_handshake() 
    File "C:\Python\lib\ssl.py", line 628, in do_handshake 
    self._sslobj.do_handshake() 
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645) 

使用證書我還得到一個錯誤:

import requests 

spiders = requests.get('https://en.wikipedia.org/w/api.php',cert = 'c:\python\lib\site-packages\certifi\cacert.pem',verify = True) 
print(spiders) 

Traceback (most recent call last): 
    File "C:/Python/Files/3.py", line 3, in <module> 
    spiders = requests.get('https://en.wikipedia.org/w/api.php',cert = 'c:\python\lib\site-packages\certifi\cacert.pem',verify = True) 
    File "C:\Python\lib\site-packages\requests\api.py", line 67, in get 
    return request('get', url, params=params, **kwargs) 
    File "C:\Python\lib\site-packages\requests\api.py", line 53, in request 
    return session.request(method=method, url=url, **kwargs) 
    File "C:\Python\lib\site-packages\requests\sessions.py", line 468, in request 
    resp = self.send(prep, **send_kwargs) 
    File "C:\Python\lib\site-packages\requests\sessions.py", line 576, in send 
    r = adapter.send(request, **kwargs) 
    File "C:\Python\lib\site-packages\requests\adapters.py", line 447, in send 
    raise SSLError(e, request=request) 
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2824) 

即使當我一直遵循一些教程時,我遇到了這個問題,當指導員有權訪問不帶問題的時候。有任何想法嗎?

+0

指定使用證書'證書='沒有任何與驗證服務器證書。這使您可以向遠程服務器提供SSL證書以進行身份​​驗證。 – larsks

回答

0

U可以忽略SSL這樣的:

import requests 

spiders = requests.get('https://en.wikipedia.org/w/api.php',verify = False) 
print(spiders) 

其他的解決辦法:

from urllib.request import Request, urlopen 
rq = Request('https://en.wikipedia.org/w/api.php') 
response = urlopen(rq).read() 
+0

工作。但我不明白爲什麼錯誤出現在請求中,但不是urllib.request。爲Python 3.5破解urllib3嗎? – Zphinx677

+0

即時通訊仍然使用python 3,2(和2.6)和urllib + urllib2所以對我來說,它的硬性檢測爲什麼urllib3如此「敏感」 –

+0

如果您執行verify = False,您將從請求中收到警告。要禁用警告:requests.packages.urllib3.disable_warnings() – RiTu