Python-requests取消SSL验证的警告InsecureRequestWarning解决办法

时间:2024-02-24 15:07:17

使用requests模块请求一个证书无效的网站的话会直接报错

可以设置verify参数为False解决这个问题

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import requests

r = requests.get(\'https://www.baidu.com/\', verify=False)
print(r.status_code)

但是设置verify=False会抛出一个InsecureRequestWarning的警告

这样看起来很不好

解决办法:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

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

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


r = requests.get(\'https://www.baidu.com/\', verify=False)
print(r.status_code)

运行结果

成功消除了警告