发布时间:2019-09-12 07:55:41编辑:auto阅读(2020)
参考文档 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系统时间
48927
48046
38809
35930
30348
27128
26130
20964
20791
19143
667°
744°
720°
717°
695°
651°
753°
835°
950°
1190°