您现在的位置是:课程教程文章

python requests检测响应状态码

2023-12-13 23:16课程教程文章 人已围观

1、为了方便引用,Requests附有一个内置的状态码查询对象。

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

2、如果发送失败请求(非200响应),我们可以通过Response.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:404ClientError

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

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

课程教程:python requests检测响应状态码

上一篇:socks5代理有什么作用?

下一篇:没有了

站点信息

  • 文章统计篇文章