当前位置:网站首页>Requests the library deployment and common function
Requests the library deployment and common function
2022-08-05 05:03:00 【Si Xiaoyou】
1.Requests常用函数讲解
import json
import jsonpath
import requests
url = "http://www.baidu.com"
res = requests.get(url)
# Print what the response came back with Binary text content
# print(res.content)
# 文本内容
# print(res.text)
# 接口地址
# print(res.url)
# cookie
# print(res.cookies)
# Print the header content
# 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()))
# Get the content that comes back in the response res.json good point 数据类型 字典类型
# res.textThe content of the response comes back 字符串的类型
# 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传
# 非要用dataCan it be passed,可以哦
# 接口文档 It is required to passcontent-type:application/json
# data A string is required to be passed
# jsonA dictionary type is required to be passed
# 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())
# When the interface documentation does not match the actual interface parameter type,就要改 改json
# The data type is defined when developing the write interface
# dataA string is required to be passed
# Manually convert the dictionary to a string
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# # dictionary to string String to dictionary 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提交 字典类型的数据
# String to dictionary
# url = "http://39.98.138.157:5000/api/login"
# data='{"password": "123456","username": "admin"}'
# # dictionary to string String to dictionary 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传参 The data is changed to string type
# json传字典 data传字符串
# String to dictionary json.loads(data) eval
# dictionary to string 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)
# The read file is a string
# f1 = open('tt.txt','r')
# print(f1.read(),type(f1.read()))
f1 = open('tt.txt','r')
ff = json.load(f1)
print(ff,type(ff))
# Login interface test
url = "http://39.98.138.157:5000/api/login"
data='{"password": "123456","username": "admin"}'
# dictionary to string String to dictionary 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())
# If this use case returns yessuccess 用例成功 用例失败
# 预期结果
exmsg = 'success'
# 实际结果 msg的字段的值 字典取值
# 其他的方式?Get actual results 正则可以 json取值 json格式的数据
# 字典 数据类型 数据格式 通过json去取值
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
# 数据,表达式
# sjmsg = res.json()['msg']
print(sjmsg)
# 判断
if exmsg == sjmsg:
print('用例成功')
else:
print('用例失败')
# 相等 啥事没有 不相等 报错 Tips are not friendly 有个友好的提示 用什么方式 There will be a friendly reminder
# 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'))
# # # under all nodesauthor As long as the author finds it.. ['吴九', '周八', '张三', '李四', '王五', '赵六', '孙七']
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 的元素 [?(@.)]is the way to write a filter expression [?(@.isbn)]Filter other to find only the content inside the expression
print(jsonpath.jsonpath(data,'$.store.book[?(@.isbn)]'))
# 语法
# $ The entire root node object
# @ 当前节点
# .或[] 子节点
# * 任意子节点
# .. 任意后代节点
3.接口关联 实现登录 下单流程
import jsonpath
import pytest
import requests
class TestCase:
token = None
# 响应 tokennumber will change 响应回来的tokennumber to be extracted 保存在变量中.放在一个地方,都能拿到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
# Note that you need to use this when adding to the shopping cart to place an orderuserid和openid 这个怎么弄
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 when getting logged intoken号
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'])
边栏推荐
- Flutter learning three-Flutter basic structure and principle
- Learning and finishing of probability theory 8: Geometric and hypergeometric distributions
- After controlling the export file in MySQL, it becomes \N. Is there any solution?
- App快速开发建设心得:小程序+自定义插件的重要性
- The production method of the powered small sailboat is simple, the production method of the electric small sailboat
- Excel Paint
- 【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
- Mini Program_Dynamic setting of tabBar theme skin
- What is ASEMI photovoltaic diode, the role of photovoltaic diode
- Flutter学习4-基本UI组件
猜你喜欢
特征预处理
动力小帆船制作方法简单,电动小帆船制作方法
App快速开发建设心得:小程序+自定义插件的重要性
Flutter真机运行及模拟器运行
for..in和for..of的区别
The solution to the failure to read channel information when dedecms generates a message in the background
WPF中DataContext作用
Application status of digital twin technology in power system
Analyses the mainstream across technology solutions
Day019 Method overriding and introduction of related classes
随机推荐
数字_获取指定位数的小数
u-boot中的u-boot,dm-pre-reloc
[8.3] Code Source - [meow ~ meow ~ meow~] [tree] [and]
Flutter学习4-基本UI组件
[Nine Lectures on Backpacks - 01 Backpack Problems]
【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
8.04 Day35-----MVC三层架构
bytebuffer use demo
write the story about us
Flutter learning - the beginning
Structured light 3D reconstruction (1) Striped structured light 3D reconstruction
App快速开发建设心得:小程序+自定义插件的重要性
human weakness
Why did you start preparing for the soft exam just after the PMP exam?
ansible各个模块详解
how to measure distance from point to face in creo
结构光三维重建(一)条纹结构光三维重建
flink reads mongodb data source
Machine Learning Overview
dedecms dream weaving tag tag does not support capital letters fix