Python中JsonPath提取器和正则提取器

作者:炫酷的腿毛! 时间:2022-08-27 14:12:20 

一、前言

我们一般在做接口关联时,会通过保存中间变量实现接口关联,在关联时就需要用到变量提取,那今天我们就介绍接口自动化中变量提取的两大神器:正则提取器和JsonPath提取器。

1.1 正则提取器

正则提取(正则表达式只能提取字符串的数据)

1、re.seach:只匹配一个值,通过下标[1]取值,没有匹配到返回None
2、re.findall:匹配多个值,返回列表list,多个值通过下标取值,没有返回None

1.2 正则示例:

import re
import requests

a = requests.get("http://www.baidu.com")
# print(a.text)

b = re.search('charset=(.*?)><meta http-equiv=X-UA-Compatible content=IE=Edge>', a.text)
print(b)
print(b.group())
print(b.groups())
print(b.group(1))

结果:

<re.Match object; span=(94, 157), match='charset=utf-8><meta http-equiv=X-UA-Compatible co>
charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge>
('utf-8',)
utf-8

匹配通配符:

我们一般用(.*?)和(.+?)来匹配我们需要提取的数值

解释:

  • . 表示任意一个字符

  • + 表示匹配它前面的表达式1次或者多次

  • * 表示匹配它前面的表达式0次或者多次

  • ? 表示匹配它前面的表达式1次或者多次

token = re.search('"token":"(.*?)",',res.text)[1]
print("token1:%s",%token)

token = re.findall('"token":"(.*?)",'res.text)
print("token2:%s",%token)

1.3 JsonPath提取器

JsonPath提取(JsonPath只能提取json格式的数据)

jsonpath.jsonpath ,返回的是一个list,通过下标取值,没有返回None

JsonPath语法

符号描述
$查询的根节点对象,用于表示一个json数据,可以是数据或者对象
@过滤器,处理的当前节点对象
*获取所有节点
.获取子节点
. .递归搜索,筛选所有符合条件的节点
?()过滤器表达式,筛选操作
[a]或者[a,b]迭代器下标,表示一个或多个数组下标

1.4 JsonPath提取器具体使用

下面使用一个JSON文档演示JSONPath的具体使用。JSON 文档的内容如下:

{
 "store": {
   "book":[
     { "category": "reference",
       "author": "Nigel Rees",
       "title": "Sayings of the Century",
       "price": 8.95
     },
     { "category": "fiction",
       "author": "J. R. R. Tolkien",
       "title": "The Lord of the Rings",
       "isbn": "0-395-19395-8",
       "price": 22.99
     }
   ],
   "bicycle": {
     "color": "red",
     "price": 19.95
   }
 }
}

1、假设变量bookJson中已经包含了这段json字符串,可以通过一下代码反序列化得到json对象:

books=json.loads(bookJson)

2、查看store下的bicycle的color属性

checkurl = "$.store.bicycle.color"
print(jsonpath.jsonpath(data, checkurl))
# 输出:['red']  

3、输出book节点中包含的所有对象

checkurl = "$.store.book[*]"
object_list = jsonpath.jsonpath(data, checkurl)
print(object_list)

#输出
[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, 
{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

4、输出book节点的第一个对象

checkurl = "$.store.book[0]"
obj = jsonpath.jsonpath(data, checkurl)
print(obj)
# 输出: ['category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]

5、输出book节点中所有对象对应的属性title值

checkurl = "$.store.book[*].title"
titles = jsonpath.jsonpath(data, checkurl)
print(titles)
# 输出: ['Sayings of the Century', 'The Lord of the Rings']

6、输出book节点中category为fiction的所有对象

checkurl = "$.store.book[?(@.category=='fiction')]"
books=jsonpath.jsonpath(data, checkurl)
print(books)
#输出
[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

7、输出book节点中所有价格小于10的对象

checkurl="$.store.book[?(@.price<10)]"
books = jsonpath.jsonpath(data, checkurl)
print(books)
# 输出: [{'category': 'reference', 'author': 'Nigel Rees', 'title':'Sayings of the Century', 'price': 8.95}]

8、输出book节点中所有含有isb的对象

checkurl = "$.store.book[?(@.isbn)]"
books = jsonpath.jsonpath(data,checkurl)
print(books)
# 输出: [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

来源:https://blog.csdn.net/weixin_44244493/article/details/129766306

标签:JsonPath,提取器,Python
0
投稿

猜你喜欢

  • 详解Python3序列赋值、序列解包

    2022-04-19 05:24:51
  • 用js更好地截取定长字符串

    2008-01-16 12:48:00
  • PHP中include和require的使用详解

    2023-10-22 03:57:03
  • python中ImageTk.PhotoImage()不显示图片却不报错问题解决

    2023-08-26 18:12:06
  • MySQL 数值类型概述int smallint tinyint

    2010-11-02 11:46:00
  • django基础学习之send_mail功能

    2023-04-01 00:16:53
  • 用CSS3将你的设计带入下个高度[译]

    2009-06-22 13:03:00
  • python中的集合及集合常用的使用方法

    2023-05-04 11:14:11
  • 远古幻想ICON 1套+创作思路

    2007-09-30 20:33:00
  • 如何在Flask中实现数据分组流程详解

    2021-11-15 06:53:59
  • pygame学习笔记(3):运动速率、时间、事件、文字

    2023-05-20 21:19:38
  • Mysql入门系列:MySQL数据目录的位置

    2008-11-24 12:59:00
  • Python列表计数及插入实例

    2023-05-26 23:41:12
  • Python编程tkinter库Canvas实现涂鸦颜色表及围棋盘示例

    2023-08-02 13:21:02
  • python日期相关操作实例小结

    2021-07-14 18:39:13
  • ajax代理程序,自动判断字符编码

    2007-11-04 13:17:00
  • Python编写条件分支代码方法

    2021-08-16 12:31:17
  • asp关键词屏蔽过滤函数代码

    2010-05-04 16:32:00
  • python 利用panda 实现列联表(交叉表)

    2022-02-21 02:24:32
  • python使用matplotlib绘图时图例显示问题的解决

    2022-11-13 16:59:47
  • asp之家 网络编程 m.aspxhome.com