网络爬虫

Json Path

2019-08-19  本文已影响0人  lvyz0207

安装

# 安装:
pip install jsonpath
# 模块提取方法
import json
form jsonpath import jsonpath

语法规则

jsonpath的语法规则.png

代码示例

import json

from jsonpath import jsonpath

if __name__ == '__main__':
    dict = {"class": {"students": [{"student_id": "1", "name": "bob", "sex": "male", "age": 6},
                                   {"student_id": "2", "name": "amy", "sex": "female", "age": 6},
                                   {"student_id": "3", "name": "pery", "sex": "male", "age": 5}],
                                   "teachers": {"teacher_id": "1", "name": "anne", "sex": "female", "age": 32}}}

    # 获取根节点下的任意name属性的值
    print(jsonpath(dict, '$..name'))  # 输出 ['bob', 'amy', 'pery', 'anne']

    # 获取teachers节点
    print(jsonpath(dict, '$.class.teachers'))  # 输出 [{'teacher_id': '1', 'name': 'anne', 'sex': 'female', 'age': 32}]

    # 获取第一个students数据
    print(jsonpath(dict, '$..students[0]'))  # 输出  [{'student_id': '1', 'name': 'bob', 'sex': 'male', 'age': 6}]

    # 获取students的第一条数据的name属性
    print(jsonpath(dict, '$..students[0].name'))  # 输出 ['bob']

    # 获取students的0,1条数据
    print(jsonpath(dict, '$..students[0,1,3]'))   # 输出 [{'student_id': '1', 'name': 'bob', 'sex': 'male', 'age': 6}, {'student_id': '2', 'name': 'amy', 'sex': 'female', 'age': 6}]
    print(jsonpath(dict, '$..students[:2]'))    # 输出 [{'student_id': '1', 'name': 'bob', 'sex': 'male', 'age': 6}, {'student_id': '2', 'name': 'amy', 'sex': 'female', 'age': 6}]

    # 获取students的最后一条数据
    print(jsonpath(dict, '$..students[-1:]'))  # 输出 [{'student_id': '3', 'name': 'pery', 'sex': 'male', 'age': 5}]
json_path定位说明.png
上一篇下一篇

猜你喜欢

热点阅读