发布时间:2019-09-12 07:55:41编辑:auto阅读(2274)
参考文档 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系统时间
51930
51704
42019
38859
33345
30315
28958
23971
23885
22251
383°
2589°
3261°
2713°
2701°
3431°
2657°
3499°
5771°
5544°