2017-04-25 92 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。它从那里运行良好。

相关问题