当前位置:网站首页>Requests库部署与常用函数讲解
Requests库部署与常用函数讲解
2022-08-05 04:59:00 【司小幽】
1.Requests常用函数讲解
import json
import jsonpath
import requests
url = "http://www.baidu.com"
res = requests.get(url)
# 打印响应回来的内容 二进制文本内容
# print(res.content)
# 文本内容
# print(res.text)
# 接口地址
# print(res.url)
# cookie
# print(res.cookies)
# 打印头部内容
# print(res.headers)
# 打印json
# print(res.json())
# 请求不带参数 请求带参数
# url = 'http://39.98.138.157:5000/api/gettomorrow'
# data = {"city":"1"}
# res = requests.get(url = url,params = data)
# print(res.text,type(res.text))
# print(res.json(),type(res.json()))
# 取响应回来的内容 res.json 好取点 数据类型 字典类型
# res.text响应回来的内容 字符串的类型
# post请求
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# print(type(data))
# # def post(url, data=None, json=None, **kwargs): 用json
# res = requests.post(url,json=data)
# print(res.json())
# 什么时候用data,什么时候用json呢? 看content-type数据类型,如果是json,要用json传
# 非要用data传可不可以,可以哦
# 接口文档 要求传的是content-type:application/json
# data 要求传的是字符串
# json要求传的是字典类型
# url = "http://39.98.138.157:5000/api/login"
# data='{"password": "123456","username": "admin"}'
# print(type(data))
# # def post(url, data=None, json=None, **kwargs): 用json
# # 加个请求头
# header = {"content-type":"application/json"}
# res = requests.post(url,data=data,headers = header)
# print(res.headers)
# print(res.json())
# 接口文档与实际接口参数类型不一致的时候,就要改 改json
# 数据类型是开发写接口的时候定义好的
# data要求传的是字符串
# 手动把字典改成字符串
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# # 字典改成字符串 字符串改成字典 eval
# data1 = json.dumps(data)
# print(type(data1))
# header = {"content-type":"application/json"}
# res = requests.post(url,data=data1,headers = header)
# # print(res.headers)
# print(res.json())
# json提交 字典类型的数据
# 字符串改成字典
# url = "http://39.98.138.157:5000/api/login"
# data='{"password": "123456","username": "admin"}'
# # 字典改成字符串 字符串改成字典 eval
# data1 = json.loads(data)
# print(type(data1))
# header = {"content-type":"application/json"}
# res = requests.post(url,json=data1,headers = header)
# # print(res.headers)
# print(res.json())
# 总结:传json数据
# 1.可以直接用json传参
# 2.如果你要用data传参 数据改成字符串类型
# json传字典 data传字符串
# 字符串改成字典 json.loads(data) eval
# 字典改成字符串 json.dumps(data)
# json.load 和 json.dump
# json.load:用于读取文件中json数据
# json.dump:用于写入json文件中
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# f = open('tt.txt','a')
# json.dump(data,f)
# 读取出来文件是字符串
# f1 = open('tt.txt','r')
# print(f1.read(),type(f1.read()))
f1 = open('tt.txt','r')
ff = json.load(f1)
print(ff,type(ff))
# 登录的接口测试
url = "http://39.98.138.157:5000/api/login"
data='{"password": "123456","username": "admin"}'
# 字典改成字符串 字符串改成字典 eval
data1 = json.loads(data)
print(type(data1))
header = {
"content-type":"application/json"}
res = requests.post(url,json=data1,headers = header)
# print(res.headers)
print(res.json())
# 如果这条用例返回来的是success 用例成功 用例失败
# 预期结果
exmsg = 'success'
# 实际结果 msg的字段的值 字典取值
# 其他的方式?取到实际结果 正则可以 json取值 json格式的数据
# 字典 数据类型 数据格式 通过json去取值
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
# 数据,表达式
# sjmsg = res.json()['msg']
print(sjmsg)
# 判断
if exmsg == sjmsg:
print('用例成功')
else:
print('用例失败')
# 相等 啥事没有 不相等 报错 提示很不友好 有个友好的提示 用什么方式 会有个友好的提示
# try:
# assert exmsg == sjmsg
# print('用例成功')
# except Exception as e:
# print('用例失败')
2.JsonPath讲解
import jsonpath as jsonpath
data={
"store": {
"book": [
{
"category": "新闻学",
"author": "张三",
"title": "图书标题1",
"price": 8.95
},
{
"category": "金融学",
"author": "李四",
"title": "图书标题2",
"price": 12.00
},
{
"category": "计算机",
"author": "王五",
"title": "图书标题3",
"isbn": "0-553-21311-3",
"price": 9.99
},
{
"category": "医学",
"author": "赵六",
"title": "图书标题4",
"price": 22.99
}
],
"phone": {
"color": "red",
"price": 1999.00,
"author": "孙七"
},
"author": "周八",
"price": 1.00
},
"author": "吴九"
}
# # # 找出book的所有author ['张三', '李四', '王五', '赵六']
# jsonpath(数据,表达式)
print(jsonpath.jsonpath(data,'$.store.book[*].author'))
# # # 所有节点下的author 只要是作者都找出.. ['吴九', '周八', '张三', '李四', '王五', '赵六', '孙七']
print(jsonpath.jsonpath(data,'$..author'))
# # store下的所有元素
print(jsonpath.jsonpath(data,'$.store'))
# # book的第3个元素
print(jsonpath.jsonpath(data,'$.store.book[2]'))
# # book的前面2个元素 切片 [开始值:结束值 不包含结束值]
print(jsonpath.jsonpath(data,'$.store.book[:2]'))
# # book的最后2个元素
print(jsonpath.jsonpath(data,'$.store.book[-2:]'))
# # book的第1个元素到第4个元素 不包含4的元素
print(jsonpath.jsonpath(data,'$.store.book[:4]'))
# # book中所有带有 isbn 的元素 [?(@.)]是过滤表达式的写法 [?(@.isbn)]过滤其他只找表示式里面的内容
print(jsonpath.jsonpath(data,'$.store.book[?(@.isbn)]'))
# 语法
# $ 整个根节点对象
# @ 当前节点
# .或[] 子节点
# * 任意子节点
# .. 任意后代节点
3.接口关联 实现登录 下单流程
import jsonpath
import pytest
import requests
class TestCase:
token = None
# 响应 token号会变 响应回来的token号要提取出来 保存在变量中。放在一个地方,都能拿到token
def test_login(self):
url = 'http://39.98.138.157:5000/api/login'
# 张三 token 2h
data = {
"password": "123456","username": "admin"}
res = requests.post(url,json=data)
print(res.json())
# 提取token 保存在变量中 类变量 其他方式 2.session中去取 3.返回值
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
TestCase.token = jsonpath.jsonpath(res.json(),'$.token')[0]
assert 'success' == sjmsg
# 注意添加购物车下单的时候需要用到这里的userid和openid 这个怎么弄
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 获得登录时的token号
header = {
"token":TestCase.token}
res = requests.get(url,headers = header)
print(res.json())
sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬' == sjname
# 选择商品
def test_shopping(self):
url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res = requests.get(url)
print(res.json())
sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888 == sjproductid
def test_cart(self):
pass
def test_order(self):
pass
if __name__ == '__main__':
pytest.main(['-sv','test_demo3.py'])
边栏推荐
- C语言-大白话理解原码,反码和补码
- Day019 方法重写与相关类的介绍
- 数字_获取指定位数的小数
- Flutter学习三-Flutter基本结构和原理
- After controlling the export file in MySQL, it becomes \N. Is there any solution?
- Day14 jenkins部署
- [8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
- 延迟加载js方式async与defer区别
- Flutter学习5-集成-打包-发布
- What is ASEMI photovoltaic diode, the role of photovoltaic diode
猜你喜欢
The log causes these pits in the thread block, you have to guard against
[cesium] 3D Tileset model is loaded and associated with the model tree
作业8.4 进程间的通信 管道与信号
[MRCTF2020] PYWebsite
mutillidae download and installation
服务器磁盘阵列
u-boot调试定位手段
【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
什么是ASEMI光伏二极管,光伏二极管的作用
u-boot debugging and positioning means
随机推荐
MySQL Foundation (1) - Basic Cognition and Operation
软件管理rpm
【cesium】元素高亮显示
【cesium】Load and locate 3D Tileset
【转】什么是etcd
结构光三维重建(一)条纹结构光三维重建
Dephi reverse tool Dede exports function name MAP and imports it into IDA
uboot开启调试打印信息
Flutter TapGestureRecognizer 如何工作
ESP32 485光照度
MySQL基础(一)---基础认知及操作
Redis - 13、开发规范
how to measure distance from point to face in creo
LeetCode:1403. 非递增顺序的最小子序列【贪心】
In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit
判断语句_switch与case
AUTOCAD——标注关联
Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
Flutter Learning 4 - Basic UI Components
七夕节赚徽章拉