python之_requests库学习_3(请求响应信息获取)
2017-11-23 14:10
447 查看
一、响应内容信息获取
1、响应状态码
2、响应头信息
还有一个特殊点,那就是服务器可以多次接受同一 header,每次都使用不同的值。但 Requests 会将它们合并,这样它们就可以用一个映射来表示出来
3、如果某些请求包含cookie,可以使用以下命令获得cookie
获取响应信息中的cookie
在发送请求时加入cookie参数
4、请求信息获取
二、重定向与请求历史
通过
使用
三、准备请求(Prepared Request)
当从
上述代码会失去
1、响应状态码
import requests r = requests.get('https://api.github.com/some/endpoint') print(r.status_code) #响应状态码 print(r.status_code==requests.codes.ok) #内置状态码查询对象 r.raise_for_status() #通过 Response.raise_for_status() 来抛出异常
2、响应头信息
import requests r = requests.get('http://httpbin.org/get') print(r.headers) #获得响应头信息 print(r.headers['Content-Type']) print(r.headers.get('Content-Length')) >>`{'X-Processed-Time': '0.000617980957031', 'Connection': 'keep-alive', 'Via': '1.1 vegur', 'Content-Length': '268', 'X-Powered-By': 'Flask', 'Date': 'Thu, 23 Nov 2017 04:13:40 GMT', 'Server': 'meinheld/0.6.1', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Content-Type': 'application/json'} >>application/json >>268`
还有一个特殊点,那就是服务器可以多次接受同一 header,每次都使用不同的值。但 Requests 会将它们合并,这样它们就可以用一个映射来表示出来
3、如果某些请求包含cookie,可以使用以下命令获得cookie
获取响应信息中的cookie
import requests r = requests.get('https://api.douban.com/v2/book/search?小王子') print(r.cookies) print(r.cookies['bid'])
在发送请求时加入cookie参数
import requests url= 'http://httpbin.org/cookies' cookies = dict(cookies_are='working') r = requests.get(url,cookies=cookies) print(r.text)
4、请求信息获取
r.request.headers
二、重定向与请求历史
import requests r = requests.get('http://github.com') print(r.history) >>[<Response [301]>]
通过
allow_redirects参数禁用重定向处理
import requests r = requests.get('http://github.com',allow_redirects=False) print(r.status_code) print(r.history) >>301 >>[]
使用
head重启重定向
import requests r = requests.head('http://github.com',allow_redirects=True) print(r.url) print(r.status_code) print(r.history)
三、准备请求(Prepared Request)
当从
API或者会话调用中收到一个
Response对象时,
request属性其实是使用了
PreparedRequest。有时在发送请求之前,你需要对
body或者
header(或者别的什么东西)做一些额外处理
from requests import Request,Session s = Session() url='https://api.douban.com/v2/book/search' data={'q':'小王子'} header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0'} req = Request('GET',url,data=data,headers=header) prepped = req.prepare() resp = s.send(prepped,stream=stream,verity=verity,proxies=proxies,cert=cert,timeout=5)#可添加上述相关信息 print(resp.status_code)
上述代码会失去
Requests Session对象的一些优势, 尤其
Session级别的状态,例如
cookie就不会被应用到你的请求上去。要获取一个带有状态的
PreparedRequest, 请用
Session.prepare_request()取代
Request.prepare()的调用
prepped = s.prepare_request(req)
相关文章推荐
- python学习笔记:使用requests库发送get与post请求
- python之_requests库学习_2(post请求参数传递)
- 使用Python的requests库作接口测试——请求对象与响应对象
- Python Requests-学习笔记(11)-请求与响应对象
- 使用Python的requests库作接口测试——请求对象与响应对象
- ThinkPHP5学习笔记(6)请求和响应
- Python Flask Web 第二课 —— 请求-响应循环
- web 学习笔记8-HTTP协议,请求头,响应头
- python3爬虫(一)requests库的学习
- Ajax学习摘录之第八部分 在请求和响应中使用 XML
- HTTP学习笔记4-请求与响应结构例子
- JAVA学习笔记0010----请求和响应
- 浅析HTTP中POST和GET区别并用Python模拟其响应和请求
- Python学习记录-----批量发送post请求
- GeoServer学习手记(十):一个完整的WMS请求响应过程
- 随笔——python截取http请求报文响应头
- JSP&Servlet学习笔记-第三章:请求与响应
- Python Requests-学习笔记(4)-定制请求头和POST
- python之_requests库学习_5(超时与异常)
- SIP的请求/响应事务模型——SIP学习笔记(二)