You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下Python用NSEpy连接NSE网站遇SSLCertVerificationError求助

解决MacBook上Python连接NSE网站的SSL证书验证失败问题

问题重现

在MacBook上使用nsepy库连接NSE网站时触发SSL证书验证错误,代码如下:

import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
from datetime import date
from nsepy import get_history
sbin = get_history(symbol='SBIN',start=date(2024,4,1),
                   end=date(2024,4,4))

完整错误信息:

Traceback (most recent call last):
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/urllib3/util/ssl_.py", line 509, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 455, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 1046, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 1317, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/urllib3/connectionpool.py", line 793, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/urllib3/connectionpool.py", line 491, in _make_request
    raise new_e
urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/urllib3/connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/urllib3/util/retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www1.nseindia.com', port=443): Max retries exceeded with url: /products/dynaContent/common/productsSymbolMapping.jsp?dataType=PRICEVOLUMEDELIVERABLE&segmentLink=3&dateRange=&symbol=SBIN&series=EQ&symbolCount=1&fromDate=01-04-2024&toDate=04-04-2024 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  

  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/requests/sessions.py", line 266, in resolve_redirects
    resp = self.send(
           ^^^^^^^^^^
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users//PycharmProjects/pythonProject/pythonProject/.venv/lib/python3.12/site-packages/requests/adapters.py", line 517, in send
        raise SSLError(e, request=request)
    requests.exceptions.SSLError: HTTPSConnectionPool(host='www1.nseindia.com', port=443): Max retries exceeded with url: /products/dynaContent/common/productsSymbolMapping.jsp?
dataType=PRICEVOLUMEDELIVERABLE&segmentLink=3&dateRange=&symbol=SBIN&series=EQ&symbolCount=1&fromDate=01-04-2024&toDate=04-04-2024 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)')))

已尝试的操作:

  • 执行Installing Certificate.command和Update Shell Profile.command命令
  • 添加SSL上下文配置
  • 将NSE证书添加到钥匙串访问
  • 执行pip install --upgrade certifi升级证书

解决方案

方法1:手动指定certifi证书路径

Python 3.12可能未正确读取certifi的证书文件,直接在代码中指定CA证书路径:

import ssl
import certifi
from datetime import date
from nsepy import get_history

# 绑定certifi提供的CA证书到默认SSL上下文
ssl_context = ssl.create_default_context(cafile=certifi.where())
ssl._create_default_https_context = lambda: ssl_context

sbin = get_history(symbol='SBIN', start=date(2024,4,1), end=date(2024,4,4))

方法2:重置虚拟环境

虚拟环境可能存在证书路径隔离问题,重新创建环境并安装依赖:

# 删除现有虚拟环境
rm -rf .venv
# 创建新虚拟环境
python3.12 -m venv .venv
# 激活环境
source .venv/bin/activate
# 升级pip并安装依赖
pip install --upgrade pip
pip install nsepy certifi

方法3:使用系统根证书

直接指定Mac系统自带的根证书文件路径:

import ssl
from datetime import date
from nsepy import get_history

# 绑定系统根证书到SSL上下文
ssl_context = ssl.create_default_context(cafile='/etc/ssl/cert.pem')
ssl._create_default_https_context = lambda: ssl_context

sbin = get_history(symbol='SBIN', start=date(2024,4,1), end=date(2024,4,4))

方法4:降级Python版本

nsepy可能未适配Python 3.12,尝试切换到Python 3.11测试:

# 安装Python 3.11(可通过pyenv或官网下载)
pyenv install 3.11.9
pyenv local 3.11.9
# 创建对应虚拟环境
python -m venv .venv311
source .venv311/bin/activate
# 安装依赖
pip install nsepy certifi

内容的提问来源于stack exchange,提问作者Surya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:15:31