Python SSLError-任何隐藏警告的方法

时间:2018-10-20 09:02:21

标签: python-3.x request warnings

我在Windows和python3上wokirng。 我使用了请求模块使用以下代码访问网页

requests.get('https://github.com/')

然后我遇到了SSLError [SSL: CERTIFICATE VERIFY FAILED ]错误。然后我关闭了verify=False用于SSL证书检查的开关。

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

现在它正在浏览该网站,但返回警告。

Warning (from warnings module):
  File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 847
    InsecureRequestWarning)
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
<Response [200]>

现在我正在使用脚本,并且我不想向用户显示此警告。我有一种隐藏警告的方法,但是我不确定如何使用它。

warnings.simplefilter("ignore")

但是这会忽略所有警告,我只想特别隐藏此“ InsecureRequestWarning”。请指导我如何做。

1 个答案:

答案 0 :(得分:1)

所以我找到了隐藏警告的解决方案。这就是我所做的。

import warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning

warnings.simplefilter('ignore',InsecureRequestWarning)
requests.get('https://github.com/',verify=False)

这东西解决了我的问题。

相关问题