python jsonpath_rw_使用jsonpath解析json

本文介绍了如何利用jsonpath_rw库解析JSON数据,包括安装、基本使用方法以及在实际场景中的应用示例,如从36氪快讯接口提取信息。
摘要由CSDN通过智能技术生成

使用jsonpath,可以大大减少开发量。

为了能像写XPath一样写json路径,Stefan Goessner开发了jsonpath(https://goessner.net/articles/JsonPath/)。

安装方式:pip install jsonpath-rw。

简单使用:

from jsonpath_rw import jsonpath, parse

jsonpath_expr = parse('foo[*].baz')

print(jsonpath_expr)

print([match.value for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})])

print([str(match.full_path) for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})])

jsonpath.auto_id_field = 'id'

print([match.value for match in parse('foo[*].id').find({'foo': [{'id': 'bizzle'}, {'baz': 3}]})])

print([match.value for match in parse('a.*.b.`parent`.c').find({'a': {'x': {'b': 1, 'c': 'number one'}, 'y': {'b': 2, 'c': 'number two'}}})])

另一个例子,获取所有author字段:

dict = { "store": {

"book": [

{ "category": "reference",

"author": "Nigel Rees",

"title": "Sayings of the Century",

"price": 8.95

},

{ "category": "fiction",

"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

}

],

"bicycle": {

"color": "red",

"price": 19.95

}

}

}

from jsonpath_rw import parse

jsonpath_expr = parse('$..author')

res = jsonpath_expr.find(dict)

print([match.value for match in res])

对36氪的快讯接口解析,只要知道最终要取的字段名就好,不用写完整的字典取值,可以省不少事:

import requests

import json

from jsonpath_rw import parse

header = {

'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '

'(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'

}

url = 'https://36kr.com/api/newsflash?&per_page=20'

response = requests.get(url,

headers=header,

timeout=5

)

dict = json.loads(response.text)

jsonpath_expr = parse('$..title, description, published_at')

res = jsonpath_expr.find(dict)

print([match.value for match in res])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python jsonpath_rw 是一个用于解析 JSON 数据的库。它提供了一种方便的方式来使用 JSONPath 表达式来查询和过滤 JSON 数据。 JSONPath 是一种类似于 XPath 的查询语言,用于从 JSON 数据中提取特定的值或对象。jsonpath_rw 库提供了一套 API,使你可以使用 JSONPath 表达式来过滤和操作 JSON 数据。 要使用 jsonpath_rw,首先需要安装它。你可以使用 pip 命令来安装: ``` pip install jsonpath-rw ``` 安装完成后,你可以在 Python 代码中导入 `jsonpath_rw` 模块,并使用它来查询和过滤 JSON 数据。 下面是一个简单的示例,展示了如何使用 jsonpath_rw 来查询 JSON 数据: ```python from jsonpath_rw import jsonpath, parse data = { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } # 创建一个 JSONPath 表达式对象 expr = parse("$.store.book[0].author") # 使用表达式对象来查询数据 matches = [match.value for match in expr.find(data)] print(matches) # 输出: ['Nigel Rees'] ``` 在上面的示例中,我们创建了一个 JSONPath 表达式对象 `expr`,并使用它来查询 JSON 数据中的作者名字。最后,我们通过遍历匹配结果列表,取出具体的值进行打印。 你可以使用不同的 JSONPath 表达式来提取你需要的数据。更多关于 jsonpath_rw 库的使用方法和示例,请参考官方文档和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值