当前位置:网站首页>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'])
边栏推荐
- 【cesium】加载并定位 3D Tileset
- Day14 jenkins部署
- Talk about 20 common problems in data governance
- Mysql的redo log详解
- write the story about us
- Machine Learning Overview
- bytebuffer use demo
- App rapid development and construction experience: the importance of small programs + custom plug-ins
- About the installation of sklearn library
- Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
猜你喜欢

bytebuffer internal structure

Machine Learning Overview
![[Geek Challenge 2019]FinalSQL](/img/e4/0c8225ef7c5e7e5bdbaac2ef6fc867.png)
[Geek Challenge 2019]FinalSQL

【转】什么是etcd

二叉树基本性质+oj题解析

In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit

Flutter learning 5-integration-packaging-publish

一篇博客通关Redis技术栈

About the installation of sklearn library

What is the function of industrial-grade remote wireless transmission device?
随机推荐
University Physics---Particle Kinematics
Analyses the mainstream across technology solutions
A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?
upload上传图片到腾讯云,如何上传图片
[Geek Challenge 2019]FinalSQL
仪表板展示 | DataEase看中国:数据呈现中国资本市场
Please write the SparkSQL statement
Flutter Learning 4 - Basic UI Components
【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
8.04 Day35-----MVC三层架构
【学习笔记之菜Dog学C】动态内存管理之经典笔试题
Flutter学习4-基本UI组件
flink reads mongodb data source
作业8.4 进程间的通信 管道与信号
服务器磁盘阵列
bytebuffer use demo
write the story about us
【cesium】3D Tileset 模型加载并与模型树关联
ansible各个模块详解
Develop your own node package