怎么让网站快速收录,留号码的广告网站不需要验证码,seo诊断a5,2023小规模企业所得税怎么征收的一、数据提取概述
知识点
了解 响应内容的分类了解 xml和html的区别 1. 响应内容的分类 在发送请求获取响应之后#xff0c;可能存在多种不同类型的响应内容#xff1b;而且很多时候#xff0c;我们只需要响应内容中的一部分数据 结构化的响应内容 json字符串 可以使用re、…一、数据提取概述
知识点
了解 响应内容的分类了解 xml和html的区别 1. 响应内容的分类 在发送请求获取响应之后可能存在多种不同类型的响应内容而且很多时候我们只需要响应内容中的一部分数据 结构化的响应内容 json字符串 可以使用re、json等模块来提取特定数据json字符串的例子如下图 xml字符串 可以使用re、lxml等模块来提取特定数据 xml字符串的例子如下 bookstore
book categoryCOOKINGtitle langenEveryday Italian/title authorGiada De Laurentiis/author year2005/year price30.00/price
/book
book categoryCHILDRENtitle langenHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price
/book
book categoryWEBtitle langenLearning XML/title authorErik T. Ray/author year2003/year price39.95/price
/book
/bookstore非结构化的响应内容 html字符串 可以使用re、lxml等模块来提取特定数据html字符串的例子如下图 知识点了解 响应内容的分类 2. 认识xml以及和html的区别 要搞清楚html和xml的区别首先需要我们来认识xml 2.1 认识xml xml是一种可扩展标记语言样子和html很像功能更专注于对传输和存储数据 bookstore
book categoryCOOKINGtitle langenEveryday Italian/title authorGiada De Laurentiis/author year2005/year price30.00/price
/book
book categoryCHILDRENtitle langenHarry Potter/title authorJ K. Rowling/author year2005/year price29.99/price
/book
book categoryWEBtitle langenLearning XML/title authorErik T. Ray/author year2003/year price39.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 jsonpath
ret 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 # 返回列表如果取不到将返回False3. 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, headersheaders)
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_asciiFalse)f.write(content)知识点掌握 jsonpath模块的使用 ent json.dumps(citylist, ensure_asciiFalse) f.write(content)
----##### 知识点掌握 jsonpath模块的使用----