当前位置:网站首页>【爬虫】数据提取之JSONpath
【爬虫】数据提取之JSONpath
2022-07-04 22:46:00 【语音不识别】
安装
pip install jsonpath
# 或者
pip install jsonpath -i https://pypi.tuna.tsinghua.edu.cn/simple
使用
from jsonpath import jsonpath
ret = jsonpath(json_dict, 'jsonpath语法规则字符串')
jsonpath语法规则
JSONPath | 描述 |
---|---|
$ | 表示根元素 |
@ | 当前元素 |
. or[] | 子元素 |
n/a | 父元素,jsonpath未支持 |
.. | 不管位置,选择符合条件的元素 |
* | 匹配所有元素节点 |
n/a | 根据属性访问,jsonpath未支持,因为json是一个key:value的递归结果,不需要属性访问。 |
[] | 迭代器标示,可以在里面做简单的迭代操作,如数组下标、根据内容选值等。 |
[,] | 支持迭代器做多选 |
?() | 支持过滤操作 |
() | 支持表达式计算 |
n/a | 分组,jsonpath未支持 |
举例
from jsonpath import jsonpath
book_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
}
}
}
res = jsonpath(book_dict, '$.store.book[*].author ')
print(res)
#索引从0开始 [索引]
res = jsonpath(book_dict , '$.store.book[0]')
print(res)
#根据条件筛选 ?(@.字段>数值)
# .. 直达选中字段
res = jsonpath(book_dict, '$..book[?(@.price>10)]')
print(res)
JSONPath | 描述 |
---|---|
$.store.book[*].author | 获取store中所有的book的作者 |
$..author | 获取所有的作者 |
$.store.* | 获取store下的所有的元素 |
$.store..price | 获取store中的所有的图书的价格 |
$..book[2] | 获取第三本书 |
$..book[(@.length-1)] | $..book[-1:] | 获取最后一本书 |
$..book[0,1]| $..book[:2] | 获取前两本书 |
$..book[?(@.isbn)] | 获取有isbn的所有书 |
$..book[?(@.price>10)] | 获取价格大于10的所有图书 |
$..* | 获取所有的数据 |
边栏推荐
- 位运算符讲解
- Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
- Attack and defense world misc advanced area Hong
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- The small program vant tab component solves the problem of too much text and incomplete display
- Network namespace
- UML图记忆技巧
- The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
- UML diagram memory skills
- Principle of lazy loading of pictures
猜你喜欢
vim编辑器知识总结
Redis入门完整教程:发布订阅
One of the commonly used technical indicators, reading boll Bollinger line indicators
Redis:Redis消息的发布与订阅(了解)
攻防世界 MISC 进阶区 3-11
Explanation of bitwise operators
Redis入门完整教程:Pipeline
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
Redis:Redis的事务
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
随机推荐
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Redis入门完整教程:API的理解和使用
How to send a reliable request before closing the page
Insert sort, select sort, bubble sort
The difference between Max and greatest in SQL
JS 3D explosive fragment image switching JS special effect
Redis入門完整教程:Pipeline
Redis入门完整教程:Redis使用场景
PS style JS webpage graffiti board plug-in
Redis introduction complete tutorial: Collection details
How to choose a securities company? Is it safe to open an account on your mobile phone
云服务器设置ssh密钥登录
ffmpeg快速剪辑
【机器学习】手写数字识别
Detailed explanation of heap sort code
One of the commonly used technical indicators, reading boll Bollinger line indicators
Three stage operations in the attack and defense drill of the blue team
Redis introduction complete tutorial: detailed explanation of ordered collection