Jupyter Notebook中出现SSLCertVerificationError问题求助
解决SSLCertVerificationError问题
问题原因
你代码里写的是http://链接,但目标服务器会自动将HTTP请求重定向到HTTPS,此时urllib发起HTTPS请求时,证书验证失败导致报错。
解决方案
方案1:改用HTTPS并跳过证书验证(测试场景用)
直接修改链接为HTTPS,同时创建不验证证书的上下文:
from urllib.request import urlopen import ssl context = ssl._create_unverified_context() html = urlopen('https://pythonscraping.com/pages/page1.html', context=context) print(html.read())
注意:跳过证书验证存在安全风险,仅在测试或完全信任目标网站时使用。
方案2:更新系统根证书(生产环境优先)
如果不想跳过验证,确保系统根证书是最新的:
- Debian/Ubuntu 系统:
sudo apt-get update && sudo apt-get install --reinstall ca-certificates
- RHEL/CentOS 系统:
sudo yum reinstall ca-certificates
- Windows 系统:通过系统设置更新根证书(设置→更新和安全→Windows更新)
方案3:改用requests库处理(更简便)
requests库对证书问题的处理更灵活,两种方式可选:
- 跳过验证:
import requests response = requests.get('https://pythonscraping.com/pages/page1.html', verify=False) print(response.text)
- 指定自定义证书(如果有需要):
import requests response = requests.get('https://pythonscraping.com/pages/page1.html', verify='/path/to/your/certificate.pem') print(response.text)
内容的提问来源于stack exchange,提问作者Indian History
相关产品推荐
相关产品推荐

