news 2026/3/18 1:14:19

macOS系统上或首次使用Python的urllib模块时出现 ssl.SSLCertVerificationError 错误

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
macOS系统上或首次使用Python的urllib模块时出现 ssl.SSLCertVerificationError 错误

macOS系统上或首次使用Python的urllib模块时出现 ssl.SSLCertVerificationError 错误

今天在macOS系统上运行如下的Python代码时报错:

fromurllib.requestimporturlopenfrombs4importBeautifulSoup html=urlopen('http://www.pythonscraping.com/pages/page1.html')bs=BeautifulSoup(html.read(),'html.parser')print(bs.h1)

首先我在macOS中运行pip install beautifulsoup4安装了beautifulsoup4库,并且同样的代码在Windows11上正常运行,所以代码没有什么问题,后面Google搜索了一下找到了解决方法。

报错如下:

$ python scrapetest.py Traceback(most recent call last): File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line1319,indo_open h.request(req.get_method(), req.selector, req.data, headers, ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^encode_chunked=req.has_header('Transfer-encoding'))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line1338,inrequest self._send_request(method, url, body, headers, encode_chunked)~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line1384,in_send_request self.endheaders(body,encode_chunked=encode_chunked)~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line1333,inendheaders self._send_output(message_body,encode_chunked=encode_chunked)~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line1093,in_send_output self.send(msg)~~~~~~~~~^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line1037,insend self.connect()~~~~~~~~~~~~^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line1479,inconnect self.sock=self._context.wrap_socket(self.sock, ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^server_hostname=server_hostname)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line455,inwrap_socketreturnself.sslsocket_class._create(~~~~~~~~~~~~~~~~~~~~~~~~~~~~^sock=sock, ^^^^^^^^^^...<5lines>...session=session ^^^^^^^^^^^^^^^)^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line1076,in_create self.do_handshake()~~~~~~~~~~~~~~~~~^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line1372,indo_handshake self._sslobj.do_handshake()~~~~~~~~~~~~~~~~~~~~~~~~~^^ ssl.SSLCertVerificationError:[SSL: CERTIFICATE_VERIFY_FAILED]certificate verify failed: unable to getlocalissuer certificate(_ssl.c:1028)During handling of the above exception, another exception occurred: Traceback(most recent call last): File"/Users/chufengcao/WorkSpace/PythonProjects/python-scraping-demo-ccf/scrapetest.py", line4,in<module>html=urlopen('http://www.pythonscraping.com/pages/page1.html')File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line189,inurlopenreturnopener.open(url, data,timeout)~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line495,inopenresponse=meth(req, response)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line604,inhttp_response response=self.parent.error('http', request, response, code, msg, hdrs)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line527,inerror result=self._call_chain(*args)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line466,in_call_chain result=func(*args)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line720,inhttp_error_302returnself.parent.open(new,timeout=req.timeout)~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line489,inopenresponse=self._open(req, data)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line506,in_open result=self._call_chain(self.handle_open, protocol, protocol +'_open', req)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line466,in_call_chain result=func(*args)File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line1367,inhttps_openreturnself.do_open(http.client.HTTPSConnection, req, ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^context=self._context)^^^^^^^^^^^^^^^^^^^^^^ File"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line1322,indo_open raise URLError(err)urllib.error.URLError:<urlopen error[SSL: CERTIFICATE_VERIFY_FAILED]certificate verify failed: unable to getlocalissuer certificate(_ssl.c:1028)>

关键错误信息为:urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1028)>

问题原因

macOS 上的 Python 可能无法访问系统默认证书。最可靠的解决方法通常是运行 Python 安装包中提供的特定脚本来安装或更新 certifi 包,该包提供了一个经过整理的根证书列表。

解决方法

macOS 上的 Python 可能无法访问系统默认证书。最可靠的解决方法通常是运行 Python 安装包中提供的特定脚本来安装或更新 certifi 包,该包提供了一个经过整理的根证书列表。

  • 1.打开终端。

  • 2.运行以下命令(该命令专门用于 macOS 上的 Python 安装):

/Applications/Python\3.13/Install\Certificates.command

注意:如果您以不同的方式安装 Python,路径可能会略有不同。从您的回溯信息来看,路径 /Library/Frameworks/Python.framework/Versions/3.13/ 可能是 Python 的安装位置。)运行此脚本后,请再次尝试运行您的python scrapetest.py脚本。

重新运行python scrapetest.py脚本,得到如下的结果:

<h1>An Interesting Title</h1>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/15 11:05:58

IINA播放器完整教程:macOS视频播放的终极解决方案

IINA播放器完整教程&#xff1a;macOS视频播放的终极解决方案 【免费下载链接】iina 项目地址: https://gitcode.com/gh_mirrors/iin/iina 在现代数字生活中&#xff0c;视频播放已成为我们日常娱乐和工作的重要组成部分。作为macOS平台上备受赞誉的开源视频播放器&…

作者头像 李华
网站建设 2026/3/15 15:34:14

基于MCU的有源蜂鸣器和无源区分驱动电路实践

有源蜂鸣器 vs 无源蜂鸣器&#xff1a;MCU驱动实战避坑指南你有没有遇到过这种情况&#xff1f;代码写得没问题&#xff0c;GPIO也配置了输出&#xff0c;可一通电——蜂鸣器要么“啪”地响一下就停&#xff0c;要么干脆一声不吭。更离谱的是&#xff0c;换了个型号的蜂鸣器&am…

作者头像 李华
网站建设 2026/3/14 8:23:35

Dify镜像支持Webhook回调通知外部系统

Dify 镜像支持 Webhook 回调通知外部系统 在企业 AI 应用从“能用”迈向“好用”的过程中&#xff0c;一个关键挑战浮出水面&#xff1a;如何让 AI 的决策真正驱动业务流程&#xff1f; 过去&#xff0c;大模型应用常被当作孤立的问答工具——用户提问、AI 回答、交互结束。结果…

作者头像 李华
网站建设 2026/3/15 10:27:52

9、Express 中间件与路由全解析

Express 中间件与路由全解析 1. Express 中间件概述 Express 仅自带一个中间件,即 express.static ,不过在实际开发中,我们会安装并使用大量其他中间件。以下是一些由 Express 团队维护的常用中间件: - body-parser :用于解析请求体,比如用户提交表单时。更多信息可…

作者头像 李华
网站建设 2026/3/13 6:59:15

【建议收藏】无卡党、少卡党如何在2026年做 LLM 研究的生存指南?

这是一篇关于无卡党、少卡党如何在2026年做 LLM 研究的生存指南。 先给结论。 没卡就不要去卷预训练&#xff0c;那是神仙打架&#xff1b;也不要去卷通用的指令微调&#xff0c;那是大厂的基建。现在的 模型能力已经溢出&#xff0c;但怎么用好模型、怎么解释模型、怎么给模型…

作者头像 李华
网站建设 2026/3/16 6:02:28

BongoCat终极使用指南:打造你的专属桌面猫咪伴侣

BongoCat终极使用指南&#xff1a;打造你的专属桌面猫咪伴侣 【免费下载链接】BongoCat 让呆萌可爱的 Bongo Cat 陪伴你的键盘敲击与鼠标操作&#xff0c;每一次输入都充满趣味与活力&#xff01; 项目地址: https://gitcode.com/gh_mirrors/bong/BongoCat 你是否曾幻想…

作者头像 李华