HTTPS连接池SSL证书验证错误

时间:2019-05-14 01:10:14

标签: python python-requests ssl-certificate

我正在尝试使用Python 3和Requests库抓取一个房地产列表网站

url = 'https://www.mlslistings.com'
page = requests.get(url)

,我遇到以下错误:

SSLError: HTTPSConnectionPool(host='www.mlslistings.com', port=443): Max retries exceeded with url: /Search/Result/299ae029-54cd-404d-bf6c-edab2dc896cc/1 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)')))

我可以通过

解决问题
url = 'https://www.mlslistings.com'
page = requests.get(url,verify=False)

但是我警告了

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)

我点击了链接,找到了对pip install certifi的推荐,然后尝试

import certifi
import urllib3
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
http.request('GET', 'https://mlslistings.com')

但是这样做给了我与上述相同的SSLError。

所以我有两个问题: 1.为什么“强烈建议”添加证书验证? 2.我可以做些什么来克服这个问题而又不仅仅放弃并使用verify=False

2 个答案:

答案 0 :(得分:0)

您似乎错过了网址中的SSL证书s。试试这个

url = 'https://www.mlslistings.com'
page = requests.get(url)

答案 1 :(得分:0)

要使此功能正常运行而不禁用验证,请使用Web浏览器转到该站点,并以base64格式导出根证书(证书路径中的顶部)。然后,当您进行呼叫设置时,请验证是否等于导出的证书的路径。

page=requests.get(url, verify='/path/to/exported/certificate.cer')

为什么要验证证书?为了确保您以安全的方式做事。