Jmeter接口关联(二)【使用jsonpath表达式提取值】与python中使用jsonpath提取值 完成接口关联

文章目录 

  • 前言
  • 一、jmeter中使用jsonpath的表达式来提取值
    • 1、使用jsonpath表达式匹配date的所有数据
    • 2、使用json提取器提取想要拿取的date值
  • 二、在Python中使用Jsonpath来完成接口的关联
    • ​​​​​​​1、jsonpath使用讲解
    •  2、jsonpath在接口关联中使用
  • 总结


前言

Jmeter中关于Jsonpath的使用,来提取值。

前面我们使用了jsonpath中的一种方式来提取值(按照层级方式),这种方式也是最不会出错,但是如果有些层级很深,那么就要使用很长的表达式,才可以提取出来。

那么现在我们用简单一点的方法来提取想要的值。


参考我之前写的有关jsonpath的文章:

jsonpath 语法介绍_撑着海浪游太空的博客-CSDN博客

jsonpath的表达式与json层级的区别,jsonpath里面会使用更多的符号去表示一种功能。这种类似于正则表达式。

使用了聚合数据里面的接口来做接口,已经接口关联。

聚合数据网址:聚合数据-个人中心

选择两个接口:

天气预报接口

老黄历接口

天气预报接口:

请求Key:79a35e2a5997710f3bdc86de81f21dbb

根据城市查询天气

接口地址:http://apis.juhe.cn/simpleWeather/query

请求方式:http get/post

返回类型:json

接口描述:通过城市名称或城市ID查询天气预报情况

接口调试: API测试工具

请求Header:

名称
Content-Typeapplication/x-www-form-urlencoded

请求参数说明:

名称必填类型说明
citystring要查询的城市名称/id,城市名称如:温州、上海、北京,需要utf8 urlencode
keystring在个人中心->我的数据,接口名称上方查看


老黄历接口:

请求Key:949e4265cda71e7be277a3a25e23c576

日历

接口地址:http://v.juhe.cn/laohuangli/d

请求方式:http get/post

返回类型:json/xml

接口描述:日期开始时间为2010-01-01

接口调试: API测试工具

请求Header:

名称
Content-Typeapplication/x-www-form-urlencoded

请求参数说明:

名称必填类型说明
keystring在个人中心->我的数据,接口名称上方查看
datestring日期,格式2014-09-09


一、Jmeter中按照 jsonpath表达式来提取

此例子里面的每个详细的步骤在另一篇文章:

Jmeter接口关联(一)【使用json层级方式提取值】与python中使用层级方式提取值 完成接口关联_撑着海浪游太空的博客-CSDN博客

1、使用jsonpath表达式匹配date的所有数据

2、使用json提取器提取想要拿取的date值

 


 

二、在Python中使用Jsonpath来完成接口的关联

1、jsonpath使用讲解

# 导入jsonpath文件(模块)
import jsonpath

resp_data = {'reason': '查询成功!', 'result': {'city': '上海',
                                   'realtime': {'temperature': '30', 'humidity': '90', 'info': '小雨', 'wid': '07',
                                                'direct': '西北风', 'power': '2级', 'aqi': '52'}, 'future': [
        {'date': '2023-07-10', 'temperature': '27/34℃', 'weather': '小雨转阴', 'wid': {'day': '07', 'night': '02'},
         'direct': '南风'},
        {'date': '2023-07-11', 'temperature': '28/35℃', 'weather': '阴转多云', 'wid': {'day': '02', 'night': '01'},
         'direct': '南风'},
        {'date': '2023-07-12', 'temperature': '27/35℃', 'weather': '多云转阴', 'wid': {'day': '01', 'night': '02'},
         'direct': '南风'},
        {'date': '2023-07-13', 'temperature': '27/35℃', 'weather': '阴', 'wid': {'day': '02', 'night': '02'},
         'direct': '南风'},
        {'date': '2023-07-14', 'temperature': '28/35℃', 'weather': '阴', 'wid': {'day': '02', 'night': '02'},
         'direct': '南风'}]}, 'error_code': 0}

# 使用json的层级来提取想要的值
# b=a["result"]['future'][2]["date"]

# 使用jsonpath表达式来获取想要的值
# jsonpath模块中的jsonpath函数
new_date = jsonpath.jsonpath(resp_data, "$..date")
# 打印出最后一个日期
print(new_date[-1])
C:/Users/Administrator/Desktop/learning_interface_frame/xyz_test/hh.py
2023-07-14

进程已结束,退出代码0

2、jsonpath在接口关联中使用

import requests
import jsonpath
# 天气预报的接口所需要传的参数
url1 = "http://apis.juhe.cn/simpleWeather/query"
key1 = "79a35e2a5997710f3bdc86de81f21dbb"

# 老黄历接口所需要传的参数
url2 = "http://v.juhe.cn/laohuangli/d"
key2 = "949e4265cda71e7be277a3a25e23c576"

# 把两个参数放在data字段中
data1 = {
    "city": "上海",
    "key": key1
}

# 发送接口的请求
req1 = requests.request(method="post", url=url1, data=data1)

# 把json格式的字符转化为字典格式,便于使用字典的提取方式提取值
a = req1.json()
print(a)  # 这个地方是打印出来看下返回的结果


new_time=jsonpath.jsonpath(a,"$..date")

data2 = {
    "key": key2,
    "date": new_time[-1]
}

# 请求老黄历接口
req2 = requests.request(method="post", url=url2, data=data2)
# 打印出返回的数据
print(req2.text)
C:\py\Python\python.exe C:/Users/Administrator/Desktop/learning_interface_frame/ff.py
{'reason': '查询成功!', 'result': {'city': '上海', 'realtime': {'temperature': '27', 'humidity': '96', 'info': '小雨', 'wid': '07', 'direct': '西风', 'power': '2级', 'aqi': '26'}, 'future': [{'date': '2023-07-10', 'temperature': '27/34℃', 'weather': '小雨转阴', 'wid': {'day': '07', 'night': '02'}, 'direct': '南风'}, {'date': '2023-07-11', 'temperature': '28/36℃', 'weather': '阴转多云', 'wid': {'day': '02', 'night': '01'}, 'direct': '南风'}, {'date': '2023-07-12', 'temperature': '27/36℃', 'weather': '多云转阴', 'wid': {'day': '01', 'night': '02'}, 'direct': '南风'}, {'date': '2023-07-13', 'temperature': '28/35℃', 'weather': '阴', 'wid': {'day': '02', 'night': '02'}, 'direct': '南风'}, {'date': '2023-07-14', 'temperature': '28/35℃', 'weather': '阴转小雨', 'wid': {'day': '02', 'night': '07'}, 'direct': '南风'}]}, 'error_code': 0}
{"reason":"successed","result":{"id":"4791","yangli":"2023-07-14","yinli":"癸卯(兔)年五月廿七","wuxing":"剑锋金 满执位","chongsha":"冲兔(丁卯)煞东","baiji":"癸不词讼理弱敌强 酉不宴客醉坐颠狂","jishen":"民日 天巫 月德 福德 除神 不将 续世 鸣犬","yi":"嫁娶 出行 开市 安床 入殓 启钻 安葬","xiongshen":"勾陈 天火 灾煞 血忌 五离","ji":"祈福 动土 破土"},"error_code":0}

进程已结束,退出代码0


 

总结

这篇文章中讲解了jsonpath在Jmeter中运用,和在Python中的运用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值