SSL:Windows上针对的证书\u验证\u失败错误https://downdetector.ru/

2024-05-16 14:34:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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上运行这个精确的代码时,它工作得非常好。那么这里有什么问题呢

另外,我已经阅读了关于这个话题的所有其他问题,但找不到答案


Tags: 代码httpsimportcmdweburlgetresponse
1条回答
网友
1楼 · 发布于 2024-05-16 14:34:57

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

如果将verify参数设置为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.

相关问题 更多 >