python - 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我已经阅读了考虑这个主题的所有其他问题,但找不到答案。
解决方案
这可能是因为您的计算机中没有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.
推荐阅读
- javascript - 如何将多个对象分配给一个数组?
- sql-server - 不使用实体框架的 CRUD 主细节
- amazon-web-services - 带有 lambda 函数来源的 aws cloudfront 抛出“服务器以非 JavaScript MIME 类型的“text/html”响应”
- javascript - 从 DOM 元素中获取 javascript 对象
- python-3.x - 我的熊猫数据框不能按列条件过滤
- python - 是否可以从以下每个值中删除点?
- sql - SQL:查看销售的产品包
- regex - 正则表达式帮助 - 不匹配特定字符串但不匹配其他所有内容?
- devops - 如何将变量传递到“az pipelines variable-group variable create”命令
- python - 在 Python 中使用 Groupby 进行日期时间时出现问题。如何用月/年制作条形图?