当前位置: 首页 > 图灵资讯 > 行业资讯> python requests检测响应状态码

python requests检测响应状态码

发布时间:2024-06-18 09:39:26

1、为便于引用,Requests附有内置状态码查询对象。

>>>r.status_code==requests.codes.ok
True

2、如果发送失败请求(非200响应),我们可以通过Responsee.raise_for_status()抛出异常。

>>>bad_r=requests.get('http://httpbin.org/status/404')
>>>bad_r.status_code
404

>>>bad_r.raise_for_status()
Traceback(mostrecentcalllast):
File"requests/models.py",line832,inraise_for_status
raisehttp_error
requests.exceptions.HTTPError:404Client

以上是python requests检测响应状态码的方法,希望对大家有所帮助。更多Python学习指南:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09