发布时间:2019-09-12 07:55:41编辑:auto阅读(2690)
参考文档 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系统时间
54808
41599
36178
31628
26534
26237
24762
20345
16300
15727
2805°
2568°
2652°
2796°
2743°
2969°
2878°
2722°
2727°
2699°