Python 3:urllib 模块基本功能不起作用

Posted

技术标签:

【中文标题】Python 3:urllib 模块基本功能不起作用【英文标题】:Python 3: urllib module basic function not working 【发布时间】:2021-03-01 07:34:59 【问题描述】:

我是一名新的 python 开发人员,我在 YouTube 上观看了一些教程,解释了这个模块的功能和用途,但我无法让它工作。我通过 pip 安装了模块,所以我认为这不是问题。

import urllib.request

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

print(x.read())

输出:

Traceback(最近一次调用最后一次): 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 1342 行,在 do_open h.request(req.get_method(), req.selector, req.data, headers, 请求中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1255 行 self._send_request(方法、url、正文、标头、encode_chunked) _send_request 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1301 行 self.endheaders(正文,encode_chunked=encode_chunked) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1250 行,在 endheaders self._send_output(message_body, encode_chunked=encode_chunked) _send_output 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1010 行 自我发送(味精) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 950 行,在发送中 self.connect() 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1424 行,在连接中 self.sock = self._context.wrap_socket(self.sock, wrap_socket 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”,第 500 行 返回 self.sslsocket_class._create( _create 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”,第 1040 行 self.do_handshake() 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”,第 1309 行,在 do_handshake 中 self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1122)

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“/Users/mike/PycharmProjects/urllib/main.py”,第 8 行,在 x = urllib.request.urlopen('https://www.google.com') 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 214 行,在 urlopen 返回 opener.open(url, 数据, 超时) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 517 行,打开 响应 = self._open(请求,数据) _open 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 534 行 结果 = self._call_chain(self.handle_open, 协议, 协议 + _call_chain 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 494 行 结果 = 函数(*args) https_open 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 1385 行 返回 self.do_open(http.client.HTTPSConnection, req, 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 1345 行,在 do_open 引发 URLError(err) urllib.error.URLError:

进程以退出代码 1 结束

【问题讨论】:

【参考方案1】:

Scraping: SSL: CERTIFICATE_VERIFY_FAILED error for http://en.wikipedia.org

找到解决方案:在 python 3.9 文件夹中安装“Install Certificates.command”

【讨论】:

【参考方案2】:

似乎是 SSL 证书错误。

可能是您电脑上的某个程序阻止了 SSL 证书。

【讨论】:

有什么办法可以解决 OP 的问题吗?

以上是关于Python 3:urllib 模块基本功能不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫3-----Urllib库的基本使用1

python爬虫---urllib库的基本用法

python之爬虫 Urllib库的基本使用

python爬虫从入门到放弃之 Urllib库的基本使用

python爬虫从入门到放弃之 Urllib库的基本使用

python urllib库