|
| 1 | +# JOSN 数据提取 |
| 2 | +## JSON 数据格式 |
| 3 | +### 定义 |
| 4 | + |
| 5 | +>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。 |
| 6 | + |
| 7 | +### 作用 |
| 8 | + |
| 9 | + |
| 10 | +### 获取方式 |
| 11 | +- ajax 请求接口 |
| 12 | +- 切换手机移动h5端 |
| 13 | +- app抓包获取 |
| 14 | +- 等等 |
| 15 | + |
| 16 | +### 格式化方式 |
| 17 | +- 安装 chrome 插件 JSONView |
| 18 | +- 在chrome中的请求详情中使用 preview 查看 |
| 19 | +- 使用在线JSON格式化工具,例如:https://www.bejson.com/ |
| 20 | + |
| 21 | +## JSON 模块使用 |
| 22 | +### 方法介绍 |
| 23 | + |
| 24 | + |
| 25 | +- json.loads json字符串 转 Python数据类型 |
| 26 | +- json.dumps Python数据类型 转 json字符串 |
| 27 | +- json.load json文件 转 Python数据类型 |
| 28 | +- json.dump Python数据类型 转 json文件 |
| 29 | + - ensure_ascii=False 实现让中文写入的时候保持为中文 |
| 30 | + - indent=空格数 通过空格的数量进行缩紧 |
| 31 | + |
| 32 | +### 代码演练 |
| 33 | +```python3 |
| 34 | +#!/usr/bin/python3 |
| 35 | +# -*- coding: utf-8 -*- |
| 36 | +# 导入模块 |
| 37 | +import json |
| 38 | + |
| 39 | +# json.loads json字符串 转 Python数据类型 |
| 40 | +json_string = ''' |
| 41 | +{ |
| 42 | + "name": "crise", |
| 43 | + "age": 18, |
| 44 | + "parents": { |
| 45 | + "monther": "妈妈", |
| 46 | + "father": "爸爸" |
| 47 | + } |
| 48 | +} |
| 49 | +''' |
| 50 | +print("json_string数据类型:",type(json_string)) |
| 51 | +data = json.loads(json_string) |
| 52 | +print("data数据类型:",type(data)) |
| 53 | +print(data) |
| 54 | +print("*" * 100) |
| 55 | +# json.dumps Python数据类型 转 json字符串 |
| 56 | +data = { |
| 57 | + "name": "crise", |
| 58 | + "age": 18, |
| 59 | + "parents": { |
| 60 | + "monther": "妈妈", |
| 61 | + "father": "爸爸" |
| 62 | + } |
| 63 | +} |
| 64 | +print("data数据类型:",type(data)) |
| 65 | +json_string = json.dumps(data) |
| 66 | +print("json_string数据类型:",type(json_string)) |
| 67 | +print(json_string) |
| 68 | + |
| 69 | +print("*"*100) |
| 70 | +# json.load json文件 转 Python数据类型 |
| 71 | +with open('data.json','r',encoding='utf-8') as f: |
| 72 | + data = json.load(f) |
| 73 | + print("data数据类型:", type(data)) |
| 74 | + print(data) |
| 75 | + |
| 76 | +print("*"*100) |
| 77 | +# json.dump Python数据类型 转 json文件 |
| 78 | +data = { |
| 79 | + "name": "crise", |
| 80 | + "age": 18, |
| 81 | + "parents": { |
| 82 | + "monther": "妈妈", |
| 83 | + "father": "爸爸" |
| 84 | + } |
| 85 | +} |
| 86 | +with open('data_out.json','w',encoding='utf-8') as f: |
| 87 | + json.dump(data,f,ensure_ascii=False,indent=2) |
| 88 | +``` |
| 89 | + |
| 90 | +## JsonPath |
| 91 | +### 介绍 |
| 92 | +> 用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。 |
| 93 | + |
| 94 | +``本质``: 通过一种语法规则快速从 JSON 数据中提取数据。类似于 正则表达式 通过一定规则从 text 文本内容提取数据。 |
| 95 | + |
| 96 | +### 环境搭建 |
| 97 | +#### 在线调试地址 |
| 98 | +- 使用在线调试环境 http://jsonpath.com/ |
| 99 | + |
| 100 | +#### 在线调试环境说明 |
| 101 | + |
| 102 | + |
| 103 | +#### 调试测试数据 |
| 104 | +```json |
| 105 | +{ |
| 106 | + "store": { |
| 107 | + "book": [ |
| 108 | + { "category": "reference", |
| 109 | + "author": "Nigel Rees", |
| 110 | + "title": "Sayings of the Century", |
| 111 | + "price": 8.95 |
| 112 | + }, |
| 113 | + { "category": "fiction", |
| 114 | + "author": "Evelyn Waugh", |
| 115 | + "title": "Sword of Honour", |
| 116 | + "price": 12.99 |
| 117 | + }, |
| 118 | + { "category": "fiction", |
| 119 | + "author": "Herman Melville", |
| 120 | + "title": "Moby Dick", |
| 121 | + "isbn": "0-553-21311-3", |
| 122 | + "price": 8.99 |
| 123 | + }, |
| 124 | + { "category": "fiction", |
| 125 | + "author": "J. R. R. Tolkien", |
| 126 | + "title": "The Lord of the Rings", |
| 127 | + "isbn": "0-395-19395-8", |
| 128 | + "price": 22.99 |
| 129 | + } |
| 130 | + ], |
| 131 | + "bicycle": { |
| 132 | + "color": "red", |
| 133 | + "price": 19.95 |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | +### 语法规则 |
| 139 | +| 语法 | 描述 | 案例 | |
| 140 | +|-------------|-------------------|---------------| |
| 141 | +| $ | 根节点 | |
| 142 | +| @ | 现行节点 | |
| 143 | +| . | 取子节点 | $.store.book | |
| 144 | +| .. | 取子孙节点 | $..book | |
| 145 | +| [] | 设置筛选条件 | $..book[0] | |
| 146 | +| [,] | 支持多选选择内容 | $..book[1,3] | |
| 147 | +| () | 支持表达式计算 | $..book[(@.length - 1)] | |
| 148 | +| ?() | 支持过滤操作 | $..book[?(@.price<10)] | |
| 149 | + |
| 150 | + |
| 151 | +### jsonpath 代码演练 |
| 152 | +#### 模块安装 |
| 153 | +> pip install jsonpath |
| 154 | + |
| 155 | + |
| 156 | +#### 需求 |
| 157 | +> 从 http://www.lagou.com/lbs/getAllCitySearchLabels.json 接口返回数据中提取所有的城市信息 |
| 158 | + |
| 159 | +#### 实现步骤 |
| 160 | +1. 网络获取数据 |
| 161 | +2. 把响应数据转换成python数据类型 |
| 162 | +3. 使用 jsonpath 提取数据 |
| 163 | + |
| 164 | +### 代码实现 |
| 165 | +```python3 |
| 166 | +#!/usr/bin/python3 |
| 167 | +# -*- coding: utf-8 -*- |
| 168 | + |
| 169 | +# 网络获取数据 |
| 170 | +import requests |
| 171 | +import json |
| 172 | +import jsonpath |
| 173 | + |
| 174 | +url = "http://www.lagou.com/lbs/getAllCitySearchLabels.json" |
| 175 | +headers = { |
| 176 | + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" |
| 177 | +} |
| 178 | + |
| 179 | +response = requests.get(url,headers=headers) |
| 180 | +html = response.text |
| 181 | +print(html) |
| 182 | +# 把响应数据转换成python数据类型 |
| 183 | +data = json.loads(html) |
| 184 | + |
| 185 | +# 使用 jsonpath 提取数据 |
| 186 | +cities = jsonpath.jsonpath(data,'$..name') |
| 187 | +print(cities) |
| 188 | +``` |
| 189 | + |
| 190 | +## 总结 |
| 191 | +- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 |
| 192 | +- JSON相关的方法 |
| 193 | + - json.loads json字符串 转 Python数据类型 |
| 194 | + - json.dumps Python数据类型 转 json字符串 |
| 195 | + - json.load json文件 转 Python数据类型 |
| 196 | + - json.dump Python数据类型 转 json文件 |
| 197 | +- jsonpath 是一种语法规则快速从 JSON 数据中提取数据。 |
| 198 | +- jsonpath 基本语法 |
| 199 | + - $ 根节点 |
| 200 | + - . 下一个节点 |
| 201 | + - .. 子孙节点 |
| 202 | + - [] 筛选条件,可以编写下标 |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
0 commit comments