Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
urllib.request.urlopen每次在Python 3.6中都会给我错误_Python_Python 3.x_Urllib - Fatal编程技术网

urllib.request.urlopen每次在Python 3.6中都会给我错误

urllib.request.urlopen每次在Python 3.6中都会给我错误,python,python-3.x,urllib,Python,Python 3.x,Urllib,这是我的代码,非常简单。 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.

这是我的代码,非常简单。

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)>
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6/Users/myName/Library/Preferences/PyCharmCE2017.1/scratch/scratch_3.py
回溯(最近一次呼叫最后一次):
dou open中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第1318行
encode_chunked=req.has_头('Transfer-encoding'))
请求中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1239行
self.\u发送\u请求(方法、url、正文、标题、编码\u分块)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1285行,在发送请求中
self.endheaders(body,encode\u chunked=encode\u chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1234行,在endheaders中
self.\u发送\u输出(消息体,encode\u chunked=encode\u chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1026行,在发送输出中
self.send(msg)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第964行,在send中
self.connect()
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1400行,在connect中
服务器\主机名=服务器\主机名)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第401行,在wrap_套接字中
_上下文=自身,_会话=会话)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第808行,在__
self.do_握手
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第1061行,在do_握手中
赛尔夫:握手
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第683行,在do_握手中
赛尔夫:握手
ssl.SSLError:[ssl:CERTIFICATE\u VERIFY\u FAILED]证书验证失败(\u ssl.c:749)
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Users/MattAltro/Library/Preferences/PyCharmCE2017.1/scratches/scratch_3.py”,第3行,在
x=urllib.request.urlopen('https://www.google.com/')
urlopen中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第223行
返回opener.open(url、数据、超时)
打开文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第526行
响应=自身打开(请求,数据)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第544行,打开
"开放",
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第504行,在调用链中
结果=func(*args)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第1361行,https_open
上下文=self.\u上下文,检查主机名=self.\u检查主机名)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第1320行,打开
引发URL错误(err)
urllib.error.urleror:
进程已完成,退出代码为1


请帮忙

看来您的
SSL
存在安全问题。 它对我有用

  • 它是否总是产生相同的错误
  • 当您进入
    https://www.google.com/
    在您的浏览器中?如果不是,这不是python问题,而是连接问题
  • 在某些情况下,您希望使用其他参数禁用安全检查
    verify=False

看起来您使用的是OS X和Python 3.6。Python3.6需要安装后步骤才能使SSL正常工作。显然有两种选择:

  • 安装certifi软件包
  • 运行/Applications/Python 3.6/Install Certificates.com命令

我的python是通过自制软件安装的,所以我没有.command文件,这只是用certifi软件包尝试的。该软件包可以很好地安装,但SSL仍然无法工作。最后我下载了Python3.6()并运行Install Certificates.com命令。从那以后,它工作得很好。

是的,它工作得很好。如何检查浏览器中的锁定?我在mac电脑上这会有问题吗?