在python中使用请求时无法获取本地颁发者证书

2024-05-12 21:33:02 发布

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

这是我的密码

import requests;
url='that website';
headers={
  'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
};
r = requests.get(url,headers=headers);
print(r);
print(r.status_code);

然后它遇到了这个:

requests.exceptions.SSLError: HTTPSConnectionPool(host='www.xxxxxx.com', port=44 3): Max retries exceeded with url: xxxxxxxx (Caused by SSLEr ror(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)')))

我该怎么办?


Tags: imageimporturl密码getthatapplicationwebsite
2条回答

不建议在组织的环境中使用verify = False。这实际上是在禁用SSL验证。

有时,当您在公司代理后面时,它会用代理的证书链替换证书链。在certifi使用的cacert.pem中添加证书可以解决此问题。我也有类似的问题。我是这么做的,为了解决这个问题-

  1. 找到cacert.pem所在的路径-

Install certifi, if you don't have. Command: pip install certifi

import certifi
certifi.where()
C:\\Users\\[UserID]\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\certifi\\cacert.pem
  1. 在浏览器上打开URL。从URL下载证书链并保存为Base64编码的.cer文件。

  2. 现在在记事本中打开cacert.pem,并在最后添加所有下载的证书内容(---Begin Certificate--- *** ---End Certificate---)。

快速修复:

requests.get('https://example.com', verify=False)

希望有用。

相关问题 更多 >