首页 > 解决方案 > SSL:Windows 上的 CERTIFICATE_VERIFY_FAILED 错误 https://downdetector.ru/

问题描述

我正在尝试在 cmd Windows10 中运行一个简单的网络抓取代码:

import requests
url = 'https://downdetector.ru/ne-rabotaet/tinkoff-bank/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0', 'Referrer': 'downdetector.ru'}
response = requests.get(url, 'html.parser', headers=headers)
print(response)

并得到一个错误:

SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)'))

最奇怪的是,当我的同事在他的 MacOS 上运行这段代码时,它运行得非常好。那么这里可能是什么问题呢?

PS我已经阅读了考虑这个主题的所有其他问题,但找不到答案。

标签: pythonweb-scrapingssl-certificate

解决方案


这可能是因为您的计算机中没有https://downdetector.ru中使用的证书。

如果您将验证参数设置为 False,您的代码可以工作,如下所示。知道这是不建议的。

response = requests.get(url, 'html.parser', headers=headers, verify=False)

您可以在此处找到有关验证参数的更多详细信息。https://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

Note that when verify is set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Setting verify to False may be useful during local development or testing.


推荐阅读