美文网首页
11.jsonpath模块

11.jsonpath模块

作者: M_小七 | 来源:发表于2020-07-21 21:33 被阅读0次

jsonpath模块

知识点

-了解 jsonpath模块的使用场景

-掌握 jsonpath模块的使用


1. jsonpath模块的使用场景

如果有一个多层嵌套的复杂字典,想要根据key和下标来批量提取value,这是比较困难的。jsonpath模块就能解决这个痛点,接下来我们就来学习jsonpath模块

jsonpath可以按照key对python字典进行批量数据提取


知识点:了解 jsonpath模块的使用场景

2. jsonpath模块的使用方法

2.1 jsonpath模块的安装

jsonpath是第三方模块,需要额外安装

pip install jsonpath

2.2 jsonpath模块提取数据的方法


from jsonpath import jsonpath

ret = jsonpath(a, 'jsonpath语法规则字符串')

2.3 jsonpath语法规则

jsonpath的方法.png

2.4 jsonpath使用示例


book_dict = { 

  "store": {

    "book": [ 

      { "category": "reference",

        "author": "Nigel Rees",

        "title": "Sayings of the Century",

        "price": 8.95

      },

      { "category": "fiction",

        "author": "Evelyn Waugh",

        "title": "Sword of Honour",

        "price": 12.99

      },

      { "category": "fiction",

        "author": "Herman Melville",

        "title": "Moby Dick",

        "isbn": "0-553-21311-3",

        "price": 8.99

      },

      { "category": "fiction",

        "author": "J. R. R. Tolkien",

        "title": "The Lord of the Rings",

        "isbn": "0-395-19395-8",

        "price": 22.99

      }

    ],

    "bicycle": {

      "color": "red",

      "price": 19.95

    }

  }

}

from jsonpath import jsonpath

print(jsonpath(book_dict, '$..author')) # 如果取不到将返回False # 返回列表,如果取不到将返回False

jsonpath使用示例.png

3. jsonpath练习

我们以拉勾网城市JSON文件 http://www.lagou.com/lbs/getAllCitySearchLabels.json 为例,获取所有城市的名字的列表,并写入文件。

参考代码:


import requests

import jsonpath

import json

# 获取拉勾网城市json字符串

url = 'http://www.lagou.com/lbs/getAllCitySearchLabels.json'

headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"}

response =requests.get(url, headers=headers)

html_str = response.content.decode()

# 把json格式字符串转换成python对象

jsonobj = json.loads(html_str)

# 从根节点开始,获取所有key为name的值

citylist = jsonpath.jsonpath(jsonobj,'$..name')

# 写入文件

with open('city_name.txt','w') as f:

    content = json.dumps(citylist, ensure_ascii=False)

    f.write(content)


知识点:掌握 jsonpath模块的使用

相关文章

  • 11.jsonpath模块

    jsonpath模块 知识点 -了解 jsonpath模块的使用场景 -掌握 jsonpath模块的使用 1. j...

  • python常用模块!!

    os模块: stat模块: sys模块: hashlib,md5模块: random模块: types模块: at...

  • 2018-08-19

    Angular 2 技能图谱 模块 自定义模块 根模块 特性模块 共享模块 核心模块 内置模块 Applicati...

  • 【时间管理100讲】精髓全在这里啦

    理论模块 精力管理。 行动管理。 学习模块。 高空模块。 反思模块。 运动模块。 阅读模块。 旅行模块。 人际关系...

  • python基础学习(三)

    常用模块 String模块 数学模块 随机模块 OS模块 os.path模块 re模块 常用函数及操作 列表操作 ...

  • day10-异常处理和pygame显示

    一、异常处理 1.模块 导入模块(自定义模块,第三方模块)import 模块 ---->模块.内容from 模块 ...

  • 重点知识复习(异常处理)

    1.模块 导入模块(自定义模块,第三方模块,系统其他模块)import 模块 ----> 模块.内容from 模...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • nodejs-模块

    nodejs模块 一、nodejs模块分类 1.核心模块 Core Module、内置模块、原生模块 fs模块 p...

  • Python不同网络模块网页源代码的获取

    requests模块 或者使用 selenium模块 BeautifulSoup模块 urllib模块

网友评论

      本文标题:11.jsonpath模块

      本文链接:https://www.haomeiwen.com/subject/gqzwkktx.html