1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python网络爬虫系列(五)——数据提取 jsonpath模块

python网络爬虫系列(五)——数据提取 jsonpath模块

时间:2018-09-01 19:17:31

相关推荐

python网络爬虫系列(五)——数据提取  jsonpath模块

一、数据提取概述

知识点
了解 响应内容的分类了解 xml和html的区别

1. 响应内容的分类

在发送请求获取响应之后,可能存在多种不同类型的响应内容;而且很多时候,我们只需要响应内容中的一部分数据

结构化的响应内容

json字符串

可以使用re、json等模块来提取特定数据json字符串的例子如下图

xml字符串

可以使用re、lxml等模块来提取特定数据

xml字符串的例子如下

<bookstore><book category="COOKING"><title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year></year> <price>30.00</price> </book><book category="CHILDREN"><title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year></year> <price>29.99</price> </book><book category="WEB"><title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year></year> <price>39.95</price> </book></bookstore>

非结构化的响应内容

html字符串

可以使用re、lxml等模块来提取特定数据html字符串的例子如下图

知识点:了解 响应内容的分类

2. 认识xml以及和html的区别

要搞清楚html和xml的区别,首先需要我们来认识xml

2.1 认识xml

xml是一种可扩展标记语言,样子和html很像,功能更专注于对传输和存储数据

<bookstore><book category="COOKING"><title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year></year> <price>30.00</price> </book><book category="CHILDREN"><title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year></year> <price>29.99</price> </book><book category="WEB"><title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year></year> <price>39.95</price> </book></bookstore>

上面的xml内容可以表示为下面的树结构:

2.2 xml和html的区别

二者区别如下图

html: 超文本标记语言为了更好的显示数据,侧重点是为了显示 xml: 可扩展标记语言为了传输和存储数据,侧重点是在于数据内容本身
知识点:了解 xml和html的区别

2.3 常用数据解析方法

据,侧重点是为了显示

xml: 可扩展标记语言为了传输和存储数据,侧重点是在于数据内容本身

知识点:了解 xml和html的区别

2.3 常用数据解析方法

二、数据提取-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 jsonpathret = jsonpath(a, 'jsonpath语法规则字符串')

2.3 jsonpath语法规则

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 jsonpathprint(jsonpath(book_dict, '$..author')) # 如果取不到将返回False # 返回列表,如果取不到将返回False

3. jsonpath练习

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

参考代码:

import requestsimport jsonpathimport json# 获取拉勾网城市json字符串url = '/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模块的使用

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

f.write(content)

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。