2017-04-25 89 views
1

這是我的代碼,非常簡單。urllib.request.urlopen每次都會給我錯誤Python 3.6

import urllib.request 
x = urllib.request.urlopen('https://www.google.com/') 
print(x.read()) 

和它給我這個錯誤:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/myName/Library/Preferences/PyCharmCE2017.1/scratches/scratch_3.py 
Traceback (most recent call last): 
File "/Library/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 "/Library/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 "/Library/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 "/Library/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 "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output 
    self.send(msg) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send 
    self.connect() 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect 
    server_hostname=server_hostname) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 401, in wrap_socket 
    _context=self, _session=session) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 808, in __init__ 
    self.do_handshake() 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1061, in do_handshake 
    self._sslobj.do_handshake() 
File "/Library/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 "/Users/MattAltro/Library/Preferences/PyCharmCE2017.1/scratches/scratch_3.py", line 3, in <module> 
    x = urllib.request.urlopen('https://www.google.com/') 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen 
    return opener.open(url, data, timeout) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open 
    response = self._open(req, data) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open 
    '_open', req) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain 
    result = func(*args) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open 
    context=self._context, check_hostname=self._check_hostname) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1320, in do_open 
    raise URLError(err) 
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)> 

過程與退出代碼完成1

請幫助!

回答

0

看起來您的安全存在缺陷SSL。 它適合我。

  • 它是否會一直產生相同的錯誤?
  • 當你在瀏覽器中轉到https://www.google.com/時,你有鎖嗎?如果沒有,這不是一個python問題,而是一個連接問題。
  • 在某些情況下,您希望使用其他參數verify=False禁用安全檢查。
+0

是的,它的確如此。 我該如何檢查瀏覽器中的鎖?即時通訊在Mac上可能是一個問題? – mattpython

0

看起來你在OS X和Python 3.6上。 Python 3.6需要一個安裝後步驟來使SSL工作。有兩個選項明顯:

  • 安裝CERTIFI包
  • 運行/應用/ Python的3.6 /安裝Certificates.command

我已通過自制軟件安裝在我的蟒蛇,所以我沒有擁有.command文件,並且只用certifi軟件包進行了嘗試。該軟件包安裝正常,但SSL仍然無法正常工作。我結束了下載Python 3.6(from python.org)並運行Install Certificates.command。它從那裏運行良好。

相關問題