关于Python-第三方库requests与response

requests库l6种方法:

1 requests.get(‘url’) #GET请求
2 requests.post(“url”) #POST请求
3 requests.put(“url”) #PUT请求
4 requests.delete(“url”) #DELETE请求
5 requests.head(“url”) #HEAD请求
6 requests.options(“url”) #OPTIONS请求

在使用requests库时,会经常用到response对象,它存储了服务器响应的内容

1 r.status_code #http请求的返回状态,响应状态码 200表示连接正常,404表示失败
2 r.raw #返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read() 读取
3 r.content #字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩
4 r.text #http响应内容的字符串形式,即url对应的页面内容
5 r.headers #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
6 #*特殊方法*#
7 r.json() #Requests中内置的JSON解码器
8 r.raise_for_status() #失败请求(非200响应)抛出异常
1 r.encoding #从http header中猜测响应内容的编码方式
2 
3 r.apparent_encoding #从内容中分析响应内容编码方式(备选编码方式)
4 
5 r.content #http响应内容的二进制形式

 

posted @ 2017-04-13 11:07  shapeofu  阅读(1128)  评论(0编辑  收藏  举报