- 使用场景:当接口之间有cookie数据之间的传递的情况下
- 为了确保接口之间cookie数据传递,一定要使用同一个session对象
- 接口返回的cookie数据,存储在session对象中
# 1.创建session对象
session = requests.session()
# 2.使用session对象,实现之后所有的接口请求
session.get()
session.post()
session.put()
举例
# 1.获取验证码的接口
session.get(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
# 2.使用session对象,实现登录接口
data_dict_login = {"username": "13012345678", "password": "123456", "verify_code": "8888"}
session.post(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login",data_dict_login)
# 3.使用session对象,完成查询“我的订单”接口
res_order = session.get(url="http://tpshop-test.itheima.net/Home/Order/order_list.html")
print(res_order.text)