当前位置:网站首页>[crawler] jsonpath for data extraction
[crawler] jsonpath for data extraction
2022-07-04 23:10:00 【Speech unrecognized】
install
pip install jsonpath
# perhaps
pip install jsonpath -i https://pypi.tuna.tsinghua.edu.cn/simple
Use
from jsonpath import jsonpath
ret = jsonpath(json_dict, 'jsonpath Syntax rule string ')
jsonpath Rule of grammar
JSONPath | describe |
---|---|
$ | Represents the root element |
@ | The current element |
. or[] | Subelement |
n/a | Parent element ,jsonpath Not supported |
.. | Regardless of location , Select the elements that meet the criteria |
* | Match all element nodes |
n/a | Access according to properties ,jsonpath Not supported , because json It's a key:value Recursive results of , No property access is required . |
[] | Iterator marks , You can do simple iterations inside , Such as array subscript 、 Select the value according to the content . |
[,] | Support iterators to make multiple choices |
?() | Support filtering operation |
() | Support expression evaluation |
n/a | grouping ,jsonpath Not supported |
give an example
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)
# Index from 0 Start [ Indexes ]
res = jsonpath(book_dict , '$.store.book[0]')
print(res)
# Filter by criteria ?(@. Field > The number )
# .. Go directly to the selected field
res = jsonpath(book_dict, '$..book[?(@.price>10)]')
print(res)
JSONPath | describe |
---|---|
$.store.book[*].author | obtain store All of the book The author of |
$..author | Get all the authors |
$.store.* | obtain store All elements under |
$.store..price | obtain store The price of all books in |
$..book[2] | Get the third book |
$..book[(@.length-1)] | $..book[-1:] | Get the last book |
$..book[0,1]| $..book[:2] | Get the first two books |
$..book[?(@.isbn)] | The acquisition has isbn All of my books |
$..book[?(@.price>10)] | Get a price greater than 10 All my books |
$..* | Get all the data |
边栏推荐
- P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
- Redis getting started complete tutorial: hash description
- [try to hack] wide byte injection
- Qt个人学习总结
- [Jianzhi offer] 6-10 questions
- LIst 相关待整理的知识点
- 实战模拟│JWT 登录认证
- 时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
- Redis démarrer le tutoriel complet: Pipeline
- A complete tutorial for getting started with redis: transactions and Lua
猜你喜欢
Redis入门完整教程:Pipeline
Redis introduction complete tutorial: detailed explanation of ordered collection
ETCD数据库源码分析——处理Entry记录简要流程
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
Redis入门完整教程:慢查询分析
Advanced area a of attack and defense world misc Masters_ good_ idea
D3.js+Three. JS data visualization 3D Earth JS special effect
Docker镜像的缓存特性和Dockerfile
One of the commonly used technical indicators, reading boll Bollinger line indicators
Duplicate ADMAS part name
随机推荐
Complete tutorial for getting started with redis: bitmaps
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
UML diagram memory skills
CTF竞赛题解之stm32逆向入门
Redis入門完整教程:Pipeline
Sword finger offer 68 - I. nearest common ancestor of binary search tree
[roommate learned to use Bi report data processing in the time of King glory in one game]
HMS core machine learning service
Sword finger offer 68 - ii The nearest common ancestor of binary tree
Redis入门完整教程:HyperLogLog
Feature scaling normalization
Redis:Redis消息的发布与订阅(了解)
位运算符讲解
ScriptableObject
Question brushing guide public
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
S32 Design Studio for ARM 2.2 快速入门
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
The initial arrangement of particles in SPH (solved by two pictures)
Redis入门完整教程:哈希说明