python接口忽略警告-warning

参考链接:https://www.cnblogs.com/yoyoketang/p/14214477.html

import requests

requests = requests.get('https://www.baidu.com')
print(requests)

报错:

 

 

解决方法:

import requests

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

https的请求加上verify=False忽略SSL验证后,会出现以下警告InsecureRequestWarning

/Users/xuer/PycharmProjects/test01/venv/bin/python /Users/xuer/PycharmProjects/baidutieba/tieba/333.py
/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host '127.0.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
<Response [200]>

Process finished with exit code 0

可以看出请求是成功的,但是会有warning提示

忽略警告:

法一:

import requests
requests.packages.urllib3.disable_warnings()           # 忽略警告加上这一句就ok了
requests = requests.get('https://www.baidu.com', verify=False)

print(requests)

法二:

import requests
from urllib3 import disable_warnings
disable_warnings()
requests = requests.get('https://www.baidu.com', verify=False)

print(requests)

但并不是所有的警告都是一样,还有很多各种各样的警告。所有的警告都是来自于 warnings 模块,那么忽略掉 warnings 模块的警告就可以解决
在代码前面加上这2句就可以了

参考代码:

import requests
import warnings
warnings.filterwarnings('ignore')
requests = requests.get('https://www.baidu.com', verify=False)

print(requests)

 


 

posted @ 2021-01-27 11:15  Penny悦  阅读(1415)  评论(0编辑  收藏  举报