Requests库是一个使用python语言编写,基于urllib实现的库。
可以使用requests库来发送HTTP接口请求测试,HTTP接口。
它和urlib库相比,使用起来更加的方便,可以节省工作量。但是它是通过封装urllib来实现。
安装
在线: 输入命令pip install requests来进行安装
验证方法: 在安装窗口中,输入命令python,进入交互式命令行模式
然后,输入import python,如果没有报错,就证明安装成功了
发送Get请求
# 1 导包
import requests
# 2 发送Get请求访问百度首页,并接收响应结果
response = requests.get("http://www.baidu.com")
# 3 打印响应结果
print("文本格式的响应体为:", response.text)
?
使用post请求提交表单数据
提交表单数据的方式,是在post的括号中,编写关键字参数data的值
# 导包
import requests
# 发送post请求,并接收响应数据:对XX项目登录发送请求,并获取响应数据
response = requests.post("http://localhost/index.php?m=Home&c=User&a=do_login",
data={"username":"138xxxx8006",
"password":"123456",
"verify_code":"8888"})
# 打印返回的响应数据
print("响应数据为:", response.text)
# 使用json格式打印数据
print("JSON响应体为:", response.json())
?
使用post请求提交json数据
json数据的提交,是通过设置json关键字参数的值来完成的
# 1 导包
import requests
# 2 发送post请求,提交json数据访问XX登录接口,并获取到响应数据
response = requests.post("http://xxxx/api/sys/login",
json={"mobile":"138xxxx0002","password":"123456"})
# 3 打印json格式的响应结果
print("XX登录的结果为{}".format(response.json()))
?
其他请求方式
使用requests模块发送其他请求方式时,就是利用requests.xxx来设置的
例如,发送Put请求是:requests.put(url,data=xxx)
发送Delete请求是:requests.delele(url)
别的请求也是这样
import requests
response = requests.put("http://www.baidu.com", data={"key": "value"})
response = requests.delete("http://www.baidu.com")
response = requests.head("http://www.baidu.com")
response = requests.options("http://www.baidu.com")
requests模块传递URL中查询参数有3种形式
第一种:通过URL自带的参数形式来传递
第二种:通过params参数来设置字符串形式的URL查询参数
第三种:通过params参数来设置字典形式的URL查询参数
requests模块会自动处理params中数据,并最终转化为第一种形式的参数来传递URL的查询参数
# 导包
import requests
?
# 发送带有查询参数的URL,传递URL的参数有3种方式,第一种:通过URL自带的参数来传递
response = requests.get("http://www.baidu.com/S?wd=王健林")
# 打印结果
# print("查询结果:", response.text) # 注意:不能使用response.json()来获取接口,因为它返回的html页面
# 第二种:通过params参数设置字符串形式的查询参数
response2 = requests.get("http://www.baidu.com/S", params="wd=刘强东")
# print("通过params参数设置字符串形式的查询参数:", response2.text)
# 第三种:通过params参数设置字典形式的查询参数
response3 = requests.get("http://www.baidu.com/S", params={"wd": "马云"})
print("通过params参数设置字典形式的查询参数:", response3.text)
?
# 注意:传入URL的查询参数不一定必须是get请求,post请求也可以传递URL的查询参数,
# 举例:tpshop登录接口,请求方式:post
# URL:http://localhost/index.php?m=Home&c=User&a=do_login
?
# 导包
import requests
# 发送请求 1). 访问百度首页的接口`http://www.baidu.com`,获取以下响应数据
response = requests.get("http://www.baidu.com")
# 2). 获取响应状态码
print("响应状态码为:", response.status_code)
# 3). 获取请求URL
print("请求的URL为:{}".format(response.url))
# 4). 获取响应字符编码
print("响应字符编码为:%s" % response.encoding) # 返回ISO-8859-1:这是HTTP协议当中默认的编码
# 5). 获取响应头数据
print("响应头数据为:", response.headers)
# 6). 获取响应的cookie数据
print("响应的cookie数据为:", response.cookies)
# 7). 获取文本形式的响应内容
print("文本形式的响应内容为:", response.text)
# 8). 获取字节形式的响应内容
print("字节形式的响应内容", response.content)
?
# 返回的文本格式的响应数据当中,title是乱码,那么怎么解决呢?
# 扩展:可以通过字节形式的响应内容重新解码得到数据
# 以utf-8的编码格式对响应数据进行解码response.content.decode(encoding="utf-8")
# 9 解决title乱码问题:
print("解码之后的数据:", response.content.decode(encoding=‘utf-8‘))
?
# 导包
import requests
?
# 设置请求头
headers = {"Content-Type": "application/json"}
# 发送XX登录接口请求,然后发送时,把设置的请求头传递给服务器
response = requests.post(url="http://xxxx/api/sys/login",
json={"mobile": "138xxxx0002", "password": "123456"},
headers=headers)
# 打印结果
print("设置请求头之后的登录接口结果为:", response.json())
?
# 导包
import requests
?
"""
获取验证码:http://localhost/index.php?m=Home&c=User&a=verify
登录:http://localhost/index.php?m=Home&c=User&a=do_login
我的订单:http://localhost/Home/Order/order_list.html
"""
# 发送获取验证码的接口请求
response_verify = requests.get(url="http://localhost/index.php?m=Home&c=User&a=verify")
# 获取验证码返回的数据
print("验证码返回的字节码数据:", response_verify.content)
# 获取验证码返回的cookies
cookies = response_verify.cookies
print("获取到的cookie为:", cookies)
# 发送登录接口请求,并从验证码中获取到的cookie发送给服务器
response_login = requests.post(url="http://localhost/index.php?m=Home&c=User&a=do_login",
data={"username": "138xxxx8006",
"password": "123456",
"verify_code": "8888"},
cookies=cookies)
# 打印登录数据
print("登录的结果为:", response_login.json())
# 发送我的订单接口请求,也把验证码中的cookie发送给服务器
response_order = requests.get(url="http://localhost/index.php/Home/Order/order_list.html",
cookies=cookies)
# 打印我的订单页面数据
print("订单页面数据为:", response_order.text)
?
# 1 导包
import requests
?
"""
获取验证码:http://localhost/index.php?m=Home&c=User&a=verify
登录:http://localhost/index.php?m=Home&c=User&a=do_login
我的订单:http://localhost/Home/Order/order_list.html
"""
# 2 使用session发送获取验证码接口请求
# 实例化session
session = requests.Session()
resposne = session.get("http://localhost/index.php?m=Home&c=User&a=verify")
# 输出返回响应数据的cookie
print(resposne.cookies)
# 3 使用session发送登录接口请求
response_login = session.post("http://localhost/index.php?m=Home&c=User&a=do_login",
data={"username": "138xxxx0002",
"password": "123456",
"verify_code": "8888"})
# 输出登录结果
print("登录的结果为:", response_login.json())
# 4 使用session发送我的订单页面接口请求
response_order = session.get("http://localhost/Home/Order/order_list.html")
# 输出我的订单页面的结果
print("订单页面结果为:", response_order.text)
?
# 关闭Session
session.close()
?
cookie和session有区别?
在位置上不同,cookie是保存在客户端浏览器的数据;session是保存在服务器数据
安全程度不一样,cookie由于是放在客户端的数据,所以不是特别安全。而session是存放在服务端的数据,相对更加安全。
数据量不同,cookie数据量比较小(4K,一个浏览器限制了每个站点的cookie最多不超过20个(可配置));而session是存放在服务端,服务器容量有多大,session就有多大。
性能不同,如果所有数据都存放在session中,那么服务器的压力会比较大。而cookie是分散在客户端浏览器中的。所以一般来讲,只会把重要的数据存放在session,而不重要的数据存放在cookie中
原文:https://www.cnblogs.com/vic666/p/13643855.html