requests---jsonpath在接口自动化中的应用
前言
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import requests # 请求天气的url地址 url= 'http://apis.juhe.cn/simpleWeather/query' # 请求参数 data = { "city":"上海", "key":"331eab8f3481f37868378fcdc76cb7cd" } # 发送post请求 r = requests.post(url,data=data) j = r.json() print(j)
{ "reason":"查询成功!", "result":{ "city":"上海", "realtime":{ "temperature":"23", "humidity":"25", "info":"多云", "wid":"01", "direct":"西北风", "power":"2级", "aqi":"58" }, "future":[ { "date":"2023-03-15", "temperature":"10/23℃", "weather":"多云", "wid":{ "day":"01", "night":"01" }, "direct":"西南风转东北风" }, { "date":"2023-03-16", "temperature":"9/15℃", "weather":"多云转小雨", "wid":{ "day":"01", "night":"07" }, "direct":"东南风" }, { "date":"2023-03-17", "temperature":"9/13℃", "weather":"中雨转小雨", "wid":{ "day":"08", "night":"07" }, "direct":"东南风转北风" }, { "date":"2023-03-18", "temperature":"8/15℃", "weather":"多云", "wid":{ "day":"01", "night":"01" }, "direct":"北风转东南风" }, { "date":"2023-03-19", "temperature":"12/16℃", "weather":"晴", "wid":{ "day":"00", "night":"00" }, "direct":"东南风" } ] }, "error_code":0 }
data = result['result']['future'][1] print(data) ------------------------------输出结果--------------------------- {'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}
data = jsonpath.jsonpath(result,'$..[?(@.date=="2023-03-16")]') print(data) -----------------------------输出结果---------------------------- [{'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}]
# 直接获取到future的内容 data = jsonpath.jsonpath(result,'$.result.future') data1 = jsonpath.jsonpath(result,'$.reason') print(data) print(data1) -----------------------------输出结果---------------------- [[{'date': '2023-03-15', 'temperature': '10/23℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '西南风转东北风'}, {'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}, {'date': '2023-03-17', 'temperature': '9/13℃', 'weather': '中雨转小雨', 'wid': {'day': '08', 'night': '07'}, 'direct': '东南风转北风'}, {'date': '2023-03-18', 'temperature': '8/15℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '北风转东南风'}, {'date': '2023-03-19', 'temperature': '12/16℃', 'weather': '晴', 'wid': {'day': '00', 'night': '00'}, 'direct': '东南风'}]] ['查询成功!']
data = jsonpath.jsonpath(result, '$.result.future.[*].date') print(data) -----------------------------输出结果---------------------- ['2023-03-15', '2023-03-16', '2023-03-17', '2023-03-18', '2023-03-19']
data = jsonpath.jsonpath(result, '$..future[0,1]') print(data) -----------------------------输出结果---------------------- [{'date': '2023-03-15', 'temperature': '10/23℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '西南风转东北风'}, {'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}]
http://www.atoolbox.net/Tool.php?Id=792
分类:
requests
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库