jsonpath 模块对数据的操作记录

jsonpath 模块对数据的操作记录

json模块主要处理json数据

  1. 页面上爬取的数据,需要使用 loads() 方法将 json 对象转为字典
  2. 再通过下面的语法格式将需要的数据匹配出来
  3. jsonpath.jsonpath() 返回值是:列表

语法示例:

    import jsonpath

    s = '''
    { 
        "store": {
            "book": [ 
                { 
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95
                },
            ],
        "bicycle": {
        "color": "red",
        "price": 19.95
        }
    }
    }
    '''

    # 读取json, 即将json对象转换为字典类型
    s = json.loads(s)

    res = jsonpath.jsonpath(s,'此处输入匹配的语法规则')
    print(res)

语法说明

jsonpath的匹配语法英文含义中文含义
$the root object/element表示根元素或对象
@the current object/element表示当前元素或对象
. or [ ] child operator表示子元素
..recursive descent. JSONPath borrows this syntax from E4X.递归匹配某元素下所有元素
*wildcard. All objects/elements regardless their names.通配符,匹配当前元素下所有子对象和子元素。仅匹配一级
[ ]subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.是匹配当前元素下的某个索引的元素,中括号中间的值为索引,起始值为0, 不可以使用 反向索引 。可以使用 * 表示所有的其下所有元素
[, ]Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.中括号中的值为多个索引,使用逗号分隔。
[start: end: step]array slice operator borrowed from ES4.使用索引切片匹配其中的值。[]、[,]、[indexValue1: indexvalue2: step]的语法可以参考列表的操作,注意索引从0开始。
?()applies a filter (script) expression.过滤某些元素,?()的括号中间的值是元素名
()script expression, using the underlying script engine.脚本表达式,使用底层脚本引擎。
语法代码示例
# 读取json, 即将json对象转换为字典类型
s = json.loads(s)


# 书点所有书的作者
res = jsonpath.jsonpath(s, '$.store.book..author')
print(res)
# 返回值:['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']


# 所有的作者
res = jsonpath.jsonpath(s, '$..author')
print(res)
# 返回值:['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien', 'alice']


# store的所有元素。所有的book和bicycle
res = jsonpath.jsonpath(s, '$.store')
print(res)
# 返回值:[{'bicycle': {'author': 'alice', 'color': 'red', 'price': 19.95}, 'book': [{'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95, 'category': 'reference'}, {'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99, 'category': 'fiction'}, {'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99, 'category': 'fiction'}, {'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99, 'category': 'fiction'}]}]


# store里面所有东西的price
res = jsonpath.jsonpath(s, '$..price')
print(res)
# 返回值:[19.95, 8.95, 12.99, 8.99, 22.99]

# 第三个书
res = jsonpath.jsonpath(s, '$.store.book[2]')
print(res)
# 返回值:[{'author': 'Herman Melville', 'isbn': '0-553-21311-3', 'category': 'fiction', 'title': 'Moby Dick', 'price': 8.99}]

# 最后一本书
res = jsonpath.jsonpath(s, '$.store.book[(@.length-1)]')
print(res)
# 返回值:[{'author': 'J. R. R. Tolkien', 'isbn': '0-395-19395-8', 'category': 'fiction', 'title': 'The Lord of the Rings', 'price': 22.99}]

# 前面的两本书。
# 方法一:
res = jsonpath.jsonpath(s, '$.store.book[0,1]')
print(res)
# 方法二:
res = jsonpath.jsonpath(s, '$.store.book[:2]')
print(res)
# 返回值:[{'author': 'Nigel Rees', 'category': 'reference', 'title': 'Sayings of the Century', 'price': 8.95}, {'author': 'Evelyn Waugh', 'category': 'fiction', 'title': 'Sword of Honour', 'price': 12.99}]


# 筛选出所有的包含isbn的书。
res = jsonpath.jsonpath(s, '$.store.book.?(@.isbn)')
print(res)
# 返回值:[{'price': 8.99, 'isbn': '0-553-21311-3', 'title': 'Moby Dick', 'category': 'fiction', 'author': 'Herman Melville'}, {'price': 22.99, 'isbn': '0-395-19395-8', 'title': 'The Lord of the Rings', 'category': 'fiction', 'author': 'J. R. R. Tolkien'}]


# 过滤出价格低于10的书。
res = jsonpath.jsonpath(s, '$.store.book.?(@.price<10)')
print(res)
# 返回值:[{'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'}, {'author': 'Herman Melville', 'isbn': '0-553-21311-3', 'category': 'fiction', 'price': 8.99, 'title': 'Moby Dick'}]


# 所有元素。
res = jsonpath.jsonpath(s, '$..')
print(res)
# 返回值: [{'store': {'book': [{'price': 8.95, 'title': 'Sayings of the Century', 'author': 'Nigel Rees', 'category': 'reference'}, {'price': 12.99, 'title': 'Sword of Honour', 'author': 'Evelyn Waugh', 'category': 'fiction'}, {'price': 8.99, 'title': 'Moby Dick', 'author': 'Herman Melville', 'isbn': '0-553-21311-3', 'category': 'fiction'}, {'price': 22.99, 'title': 'The Lord of the Rings', 'author': 'J. R. R. Tolkien', 'isbn': '0-395-19395-8', 'category': 'fiction'}], 'bicycle': {'color': 'red', 'author': 'alice', 'price': 19.95}}}, {'book': [{'price': 8.95, 'title': 'Sayings of the Century', 'author': 'Nigel Rees', 'category': 'reference'}, {'price': 12.99, 'title': 'Sword of Honour', 'author': 'Evelyn Waugh', 'category': 'fiction'}, {'price': 8.99, 'title': 'Moby Dick', 'author': 'Herman Melville', 'isbn': '0-553-21311-3', 'category': 'fiction'}, {'price': 22.99, 'title': 'The Lord of the Rings', 'author': 'J. R. R. Tolkien', 'isbn': '0-395-19395-8', 'category': 'fiction'}], 'bicycle': {'color': 'red', 'author': 'alice', 'price': 19.95}}, [{'price': 8.95, 'title': 'Sayings of the Century', 'author': 'Nigel Rees', 'category': 'reference'}, {'price': 12.99, 'title': 'Sword of Honour', 'author': 'Evelyn Waugh', 'category': 'fiction'}, {'price': 8.99, 'title': 'Moby Dick', 'author': 'Herman Melville', 'isbn': '0-553-21311-3', 'category': 'fiction'}, {'price': 22.99, 'title': 'The Lord of the Rings', 'author': 'J. R. R. Tolkien', 'isbn': '0-395-19395-8', 'category': 'fiction'}], {'price': 8.95, 'title': 'Sayings of the Century', 'author': 'Nigel Rees', 'category': 'reference'}, 8.95, 'Sayings of the Century', 'Nigel Rees', 'reference', {'price': 12.99, 'title': 'Sword of Honour', 'author': 'Evelyn Waugh', 'category': 'fiction'}, 12.99, 'Sword of Honour', 'Evelyn Waugh', 'fiction', {'price': 8.99, 'title': 'Moby Dick', 'author': 'Herman Melville', 'isbn': '0-553-21311-3', 'category': 'fiction'}, 8.99, 'Moby Dick', 'Herman Melville', '0-553-21311-3', 'fiction', {'price': 22.99, 'title': 'The Lord of the Rings', 'author': 'J. R. R. Tolkien', 'isbn': '0-395-19395-8', 'category': 'fiction'}, 22.99, 'The Lord of the Rings', 'J. R. R. Tolkien', '0-395-19395-8', 'fiction', {'color': 'red', 'author': 'alice', 'price': 19.95}, 'red', 'alice', 19.95]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值