发布时间:2019-09-12 07:55:41编辑:auto阅读(2355)
参考文档 http://goessner.net/articles/JsonPath/


https://pypi.python.org/pypi/jsonpath/0.75
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import jsonpath
import requests
url='https://www.lagou.com/lbs/getAllCitySearchLabels.json'
resp=requests.get(url)
city_json=resp.text
# json字符串转换为python字典对象
city_dict=json.loads(city_json)
# 使用jsonpath匹配
# 获取根节点下的所有name节点的值
names=jsonpath.jsonpath(city_dict,expr='$..name')
print(names)
# 根节点下的message节点的值
message=jsonpath.jsonpath(city_dict,expr='$.message')
print(message)
# D节点下的前3个
D=jsonpath.jsonpath(city_dict,expr='$.content.data.allCitySearchLabels.D[0:3]')
print(D)
# D节点下的第2个和第4个
D=jsonpath.jsonpath(city_dict,expr='$.content.data.allCitySearchLabels.D[1,3]')
print(D)
上一篇: 将Emacs作为Python集成开发环境
下一篇: python同步windows系统时间
52390
52319
42428
39328
33791
30746
29501
24418
24279
22650
171°
170°
180°
220°
184°
296°
292°
314°
315°
368°