多层嵌套使用这个
from jsonpath import jsonpath # jsonpath里真有jsonpath模块jsonpath data = {'key1': {'key2': {'key3': {'key4': {'key5': {'key6':'python'}}}}}} #键值索引 print(data['key1']['key2']['key3']['key4']['key5']['key6']) print(jsonpath(data,'$..key6')[0]) print(jsonpath(data,'$.key1.key2.key3.key4.key5.key6')[0])
语法
$ 根节点(最外层大括号)
. 直接子节点
.. 内部任意位置 子孙节点
结果是一个列表
应用
import requests
import jsonpath
import json
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
response = requests.get('https://www.lagou.com/lbs/getAllCitySearchLabels.json', headers=headers)
dict_data = json.loads(response.content.decode())
print(jsonpath.jsonpath(dict_data, '$..name'))
浙公网安备 33010602011771号