python requests库翻译(三)-response
来源:互联网 发布:综合体检数据 编辑:程序博客网 时间:2024/05/17 02:38
一.Response的状态码
我们可以确定返回的状态码
>>> import requests
>>> r = requests.get('http://httpbin.org/get')
>>> r.status_code
200
备注:调用.status_code属性查看接口返回的状态Requests有内置的状态吗查找对象,例如:
>>> r.status_code == requests.codes.ok
True
备注:这个意思是把状态码变成布尔类型赋给r.status_code?其实我也不知道啥意思,先写着吧如果我们想发送一个错误的请求(4**客户端错误,5**服务端错误),我们可以Response.raise_for_status()来抛出异常,例如:
>>> bad_r = requests.get('http://httpbin.org/status/404')
>>> bad_r.status_code
404
>>> bad_r.raise_for_status()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 909, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: NOT FOUND for url: http://httpbin.org/status/404
但是.raise_for_status()这个方法在状态码是200的时候,返回None
二.Response的Headers
我们可以用python的字典格式来查看服务端返回的response的头文件
>>> r.headers
{
'Content-Length': '266',
'Connection': 'keep-alive'
......}注意这个字典格式的数据比较特殊,因为是为了http 头文件,根据RFC 7230,这个字典的key是不区分大小写的,比如:
>>> r.headers['connection']
'keep-alive'
>>> r.headers['Connection']
'keep-alive'
因为这个字典数据在server端也是特殊的,所以可以同样key,不同的value值多次发送给服务端,但是因为结合request请求所以在字典中就是一对一的映射关系。(我觉得这句话的意思headers因为是头文件,和请求一起发送给接口,第二次请求可能headers[key]对应value变了,但是因为和请求第二次发送给接口,所以好事一个header对应一个请求,还是一对一的映射关系)二.Cookies
如果一个respone包含了cookies,你可以快速访问:
>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)
>>> r.cookies
<RequestsCookieJar[]>
>>> type(r.cookies)
<class 'requests.cookies.RequestsCookieJar'>
备注:返回是一个类,需要用访问字典的那种格式想发送自己的cookies到服务器,可以使用参数“cookies”例如:
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
u'{\n "cookies": {\n "cookies_are": "working"\n }\n}\n'
返回的cookies在RequestsCookieJar里,它和字典类型数据很类似,但是它页提供了完成的接口用于适应多域名和路径,Cookie jars可以和请求一同传入,例如:>>> jar = requests.cookies.RequestsCookieJar()
>>> jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
#通过set()方法设置域名:domian,接口:cookies,并设置key='tasty_cookie',value:'yum'
Cookie(version=0, name='tasty_cookie', value='yum', port=None, port_specified=False, domain='httpbin.org', domain_specified=True, domain_initial_dot=False, path='/cookies', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
>>> url = 'http://httpbin.org/cookies'
>>> r = requests.get(url, cookies=jar)
#查看返回内容
>>> r.text
u'{\n "cookies": {\n "tasty_cookie": "yum"\n }\n}\n'
三.重定向和历史默认情况下,除了头文件,请求可以重定向的。
我们可以"history"来追踪重定向的response。
Response.history列表包含Response对象完整的历史,这个列表按照时间正序排序。
例如下面,Github所有http请求重定向到https:
>>> r = requests.get('http://github.com')
>>> r.url
u'https://github.com/'
>>> r.status_code
200
>>> r.history
[<Response [301]>]
如果你使用POST,GET,OPTIONS,PUT,PATCH,DELETE,你可以使用参数"allow_redirects"来控制不允许重定向。>>> r = requests.get('http://github.com', allow_redirects=False)
>>> r.status_code
301
>>> r.history
[]
如果你使用HEAD请求,也可以设置不允许重定向:>>> r = requests.head('http://github.com', allow_redirects=True)
>>> r.url
u'https://github.com/'
>>> r.history
[<Response [301]>]
四.超时你可以通过"timeout"参数来设置几秒后请求不用等待,几乎所有的生产代码都要设置”timeout“参数,要不然很容易导致程序挂起。
注意:
超时不是限制response下载时间,相反,如果接口没有返回超时提示则会抛出异常,如果没有明确设置超时,则请求不会超时。
- python requests库翻译(三)-response
- python requests库翻译(一)
- python requests库翻译(二)
- python-requests(三)
- (三)Requests库
- python requests获取response headers中的location
- python爬虫系列(三):第三方库requests 的使用
- windows下python三方requests库安装
- python爬虫(requests)库安装
- Python爬虫入门(二)requests库
- 【Python常用库 I】Requests(未完)
- python requests库 笔记
- python requests库介绍
- Python之requests库
- python安装requests库
- python库之requests
- Python Requests库
- python#WS001 requests库
- 续流二极管
- AAChartKit 一款功能极其强大的开源图表组件库
- AngularJS所有版本下载地址
- 安卓IPC(进程间通信)之AIDL基本使用
- Anroid反编译
- python requests库翻译(三)-response
- java读取.properties配置文件
- 第 13 章 MySQL的复制
- Smack4.20实现连接登陆(1)
- pip 安装 django 报错 UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f' in position 8: o
- 北大AI公开课 第二讲 嵌入式人工智能
- 上交所、深交所、中登结算的接口文档及下载链接
- netsh & winsock & 对前端的影响
- 30套JSP网站源代码合集