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

使用Requests访问网站时遭遇SSL证书验证失败问题求助

解决Python爬取时的SSL证书验证失败问题

爬取https://portal.librus.pl/rodzina/synergia/loguj时出现SSLCertVerificationError,提示certificate verify failed: unable to get local issuer certificate,浏览器可正常访问但Python无法完成证书验证,可用以下方法解决:

方法一:临时跳过证书验证(仅测试用)

适合快速验证爬虫逻辑,生产环境禁止使用,会带来安全风险。需要关闭requests的不安全请求警告,同时在get请求中添加verify=False参数:

import requests
from bs4 import BeautifulSoup

# 关闭不安全请求的警告提示
requests.packages.urllib3.disable_warnings()

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
with requests.Session() as s:
    url = "https://portal.librus.pl/rodzina/synergia/loguj"
    r = s.get(url, headers=headers, verify=False)
    print(r.content)

方法二:使用certifi提供的可信证书库

你已经导入了certifi库,直接指定verify参数为certifi的证书路径即可,这是更安全的方案:

import requests
from bs4 import BeautifulSoup
import certifi

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
with requests.Session() as s:
    url = "https://portal.librus.pl/rodzina/synergia/loguj"
    # 用certifi维护的可信证书验证
    r = s.get(url, headers=headers, verify=certifi.where())
    print(r.content)

方法三:手动导入网站证书

如果上述方法无效,可手动从浏览器导出网站证书,然后在代码中指定证书路径:

  1. 打开浏览器访问目标网站,点击地址栏的锁图标,导出证书(保存为.pem格式,比如librus_cert.pem)
  2. 在代码中指定证书路径:
import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
with requests.Session() as s:
    url = "https://portal.librus.pl/rodzina/synergia/loguj"
    # 指定本地证书文件路径
    r = s.get(url, headers=headers, verify="./librus_cert.pem")
    print(r.content)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 14:40:20