您现在的位置是:网站首页> 编程资料编程资料

Python中requests库的用法详解_python_

2023-05-26 321人已围观

简介 Python中requests库的用法详解_python_

一、requests库

requests是使用Apache2 licensed 许可证的HTTP库。比urllib模块更简洁。

Request支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应内容的编码,支持国际化的URL和POST数据自动编码。

在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作。

安装

requests是第三方库,需要独立安装:pip install requests。requests是基于urllib编写的,并且使用起来非常方便,个人推荐使用requests。

官方中文教程地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

学习之前推荐一个非常好的http测试网站:http://httpbin.org,提供非常非常完善的接口调试、测试功能~

请求

requests支持http的各种请求,比如:

  • GET: 请求指定的页面信息,并返回实体主体。
  • HEAD: 只请求页面的首部。
  • POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体。
  • PUT: 从客户端向服务器传送的数据取代指定的文档的内容。
  • DELETE: 请求服务器删除指定的页面。
  • OPTIONS: 允许客户端查看服务器的性能。

访问baidu,获取一些基本信息:

import requests response = requests.get("https://www.baidu.com")# 打开网页获取响应 print('response:', type(response))# 打印响应类型,response: print('status_code:', response.status_code)# 打印状态码 ,status_code: 200 print('cookie:', response.cookies)# 打印cookie ,cookie: ]> print('text:', response.text) # 打印字符串形式的响应体 ,text: >ç™»å½...• print('二进制content:', response.content) # 二进制content, b'\r\n\xe7\x99\xbb\xe5\xbd\x95... \r\n' print('content:', response.content.decode("utf-8")) # content: 登录...

响应

请求后响应的内容是requests.models.Response对象,需要处理后才能得到我们需要的信息。

requests自动检测编码,可以使用encoding属性查看。

无论响应是文本还是二进制内容,我们都可以用content属性获得bytes对象:

  • encoding:获取当前的编码
  • encoding = 'utf-8' :设置编码
  • headers :以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
  • requests.headers :返回发送到服务器的头信息
  • cookies :返回cookie
  • history :返回重定向信息,当然可以在请求是加上allow_redirects = false 阻止重定向
  • status_code :响应状态码
  • raw :返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()
  • ok :查看r.ok的布尔值便可以知道是否登陆成功
  • raise_for_status() :失败请求(非200响应)抛出异常
  • text: 得到的是str类型,会自动根据响应头部的字符编码进行解码。
  • content :得到的是bytes类型,需要进行解码Response_get.content.decode(),相当于Response_get.text。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
  • json(): Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常

其实使用requset.text避免乱码的方式还有一个,就是发出请求后,获取内容之前使用response.encoding属性来改变编码,例如:

response =requests.get("http://www.baidu.com") #设置响应内容的编码方式为utf-8 response.encoding="utf-8" print(response.text)

二、发送get请求

requests.get(url=url, headers=headers, params=params)
  • url:请求url地址
  • headers:请求头
  • params:查询字符串

1、一个带参数的get请求:

对于带参数的URL,传入一个dict作为params参数,如果值为None的键不会被添加到url中。

import requests #将参数写在字典里,通过params传入,params接受字典或序列 data = { "name": "hanson", "age": 24 } response = requests.get("http://httpbin.org/get", params=data) #发出一个get请求,获得响应 print(response.url) #打印url print(response.text) #打印响应内容

结果为:

http://httpbin.org/get?name=hanson&age=24 { "args": { "age": "24", "name": "hanson" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0", "X-Amzn-Trace-Id": "Root=1-5e71bb9d-79cfc9e0195befa018426f20" }, "origin": "218.106.132.130", "url": "http://httpbin.org/get?name=hanson&age=24" }

2、响应json

requests的方便之处还在于,对于特定类型的响应,例如JSON,可以直接获取:

requests里的json方法就是封装了json.loads方法。

import requests import json # 发出一个get请求 response = requests.get("http://httpbin.org/get") # text响应类型 print(type(response.text)) # 直接解析响应json(成字典) print(response.json()) # 获取响应内容后json进行解析(成字典) print(json.loads(response.text)) # 直接解析后的相应内容类型 print(type(response.json()))

控制台打印结果:

 {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '124.74.47.82', 'url': 'http://httpbin.org/get'} {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '124.74.47.82', 'url': 'http://httpbin.org/get'} < class 'dict'>

3、添加头信息headers

需要传入HTTP Header时,我们传入一个dict作为headers参数:

添加头信息访问:

import requests # 添加头部信息 headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" } # 发送请求 response = requests.get("https://www.zhihu.com", headers=headers) # 打印响应 print(response.text)

4、添加和获取cookie信息

equests对Cookie做了特殊处理,使得我们不必解析Cookie就可以轻松获取指定的Cookie:

要在请求中传入Cookie,只需准备一个dict传入cookies参数:

header = {'user-agent': 'my-app/0.0.1''} cookie = {'key':'value'} #发送请求 response = requests.get/post('your url',headers=header,cookies=cookie) #打印cookie print(response.cookies) for key, value in response.cookies.items(): print(key + "=" + value)

三、发送post请求

requests.post(url=url, headers=headers, data=params)
  • url:请求url地址
  • headers:请求头
  • data:发送编码为表单形式的数据

1、一个带参数的Post请求:

要发送POST请求,只需要把get()方法变成post(),然后传入data参数作为POST请求的数据:

import requests #参数写在字典里 data = { "name": "hason", "age": 23 } #请求时将字典参数赋给data参数 response = requests.post("http://httpbin.org/post", data=data) #打印响应 print(response.text)

打印结果:

{ "args": {}, "data": "", "files": {}, "form": { "age": "23", "name": "zhaofan" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "19", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.4" }, "json": null, "origin": "124.74.47.82, 124.74.47.82", "url": "https://httpbin.org/post" }

2、传递JSON数据

requests默认使用application/x-www-form-urlencoded对POST数据编码。如果要传递JSON数据,可以直接传入json参数:

params = {'key': 'value'} r = requests.post(url, json=params) # 内部自动序列化为JSON

3、文件上传

文件上传需要用到请求参数里的files参数:

在读取文件时,注意务必使用'rb'即二进制模式读取,这样获取的bytes长度才是文件的长度。

import requests # rb,以只读的方式打开二进制文件 files = {"files": open("a.jpg", "rb")} # 发送post请求携带文件 response = requests.post("http://httpbin.org/post", files=files) # 响应内容 print(response.text)

响应结果:

{ "args": {}, "data": "", "files": { "files": "" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "145", "Content-Type": "multipart/form-data; boundary=75c9d62b8f1
                
                

-六神源码网