其他分享
首页 > 其他分享> > Warning:InsecureRequestWarning: Unverified HTTPS request is being made

Warning:InsecureRequestWarning: Unverified HTTPS request is being made

作者:互联网

爬虫爬取的时候出现的warning,不影响使用,但是看得有点脑瓜子疼。
InsecureRequestWarning: Unverified HTTPS request is being made to host ‘webvpn.dlut.edu.cn’. Adding certificate verification is strongly advised.

尝试使用网上推荐的方法

from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

报错Unresolved reference ‘InsecureRequestWarning’
遂使用https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl官方推荐方法

简单翻译一下:
证书验证

HTTPS连接目前在默认情况下进行验证(cert\u reqs=‘cert\u REQUIRED’)。
虽然您可以通过设置cert\u reqs='cert\u NONE’来禁用证书验证,但强烈建议将其保留为打开状态。

除非另行指定,否则urllib3将尝试加载默认的系统证书存储。最可靠的跨平台方法是使用certifi包,它提供Mozilla的根证书包:

$python-m pip install certifi

或者

$ python -m pip install urllib3[secure]

拥有证书后,可以创建一个PoolManager,在发出请求时验证证书:

>>> import certifi
>>> import urllib3
>>> http = urllib3.PoolManager(
...     cert_reqs='CERT_REQUIRED',
...     ca_certs=certifi.where()

PoolManager将自动进行证书验证,并在验证失败时引发SSLError:

>>> http.request('GET', 'https://google.com')(No exception)
>>> http.request('GET', 'https://expired.badssl.com')urllib3.exceptions.SSLError ...

如果需要,可以使用操作系统提供的证书。只需将证书束的完整路径指定为ca\u certs参数,而非certifi.where(). 大多数Linux系统将证书存储在/etc/ssl/certs/ca-certificates
.crt. 其他操作系统可能较困难。


一个更简单的方法是
pip install -U requests[security]
即三个security相关的package:pyopenssl cryptography idna


标签:made,证书,being,request,urllib3,cert,InsecureRequestWarning,certifi
来源: https://blog.csdn.net/qq_41734424/article/details/112679055