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

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

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

python 复制代码
from urllib.request import urlopen
from bs4 import BeautifulSoup

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搜索了一下找到了解决方法。

报错如下:

bash 复制代码
$ python scrapetest.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 1319, in do_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", line 1338, in request
    self._send_request(method, url, body, headers, encode_chunked)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 1384, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 1333, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 1093, in _send_output
    self.send(msg)
    ~~~~~~~~~^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 1037, in send
    self.connect()
    ~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 1479, in connect
    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", line 455, in wrap_socket
    return self.sslsocket_class._create(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        sock=sock,
        ^^^^^^^^^^
    ...<5 lines>...
        session=session
        ^^^^^^^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line 1076, in _create
    self.do_handshake()
    ~~~~~~~~~~~~~~~~~^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line 1372, in do_handshake
    self._sslobj.do_handshake()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer 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", line 4, 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", line 189, in urlopen
    return opener.open(url, data, timeout)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 495, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 604, in http_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", line 527, in error
    result = self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 466, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 720, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 489, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 506, 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", line 466, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 1367, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        context=self._context)
                        ^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/urllib/request.py", line 1322, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer 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 安装):

bash 复制代码
/Applications/Python\ 3.13/Install\ Certificates.command

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

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

bash 复制代码
<h1>An Interesting Title</h1>
相关推荐
筱昕~呀10 小时前
“烷”域天观——甲烷监测系统
人工智能·python·深度学习·豆包
mahtengdbb110 小时前
YOLO11-seg-ASF-DySample:人脸检测识别新突破,提升检测精度与效率
python
测试老哥10 小时前
接口测试:加密和签名
自动化测试·软件测试·python·功能测试·测试工具·测试用例·接口测试
今天又得骑车了10 小时前
Python 3.12 内置函数全图鉴:71 个“官方外挂”详解
python
CCPC不拿奖不改名10 小时前
大语言模型的基础:大语言模型基础认知
人工智能·python·学习·语言模型·自然语言处理·面向对象·智能体
夏沫mds10 小时前
基于 Flask 与Vue 3 及协同过滤算法的智能电影推荐系统
vue.js·python·flask·协同过滤
子午10 小时前
【2026原创】鱼类识别系统~Python+深度学习+CNN卷积神经网络算法+模型训练+图像识别
图像处理·python·深度学习·cnn
地理探险家10 小时前
【YOLOv8实战】15组衣物类深度学习数据集分享|附加载+标签管理代码
人工智能·python·深度学习·yolo·模型训练·电商视觉
大学生毕业题目10 小时前
毕业项目推荐:103-基于yolov8/yolov5/yolo11的皮肤癌检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·皮肤癌检测
ai_top_trends10 小时前
AI 生成 PPT 哪个好用?2026 年主流工具实测对比
人工智能·python