1.响应分类
- 结构化
关系数据库表形式管理的数据 - 半结构化
非关系模型的、有基本固定结构模式的数据,例如日志文件、XML文档、JSON文档、Email等。
json模块、re模块、jsonpath模块、lxml模块
- 非结构化
没有固定模式的数据,如HTML、WORD、PDF、PPT、EXL,各种格式的图片、视频等
re模块、lxml模块
2.XML和HTML的区别
- xml:
(是树状结构)
可扩展标记语言
注重传输和储存数据 - html:
超文本标记语言
注重显示和如何更好地显示数据
3.jsonpath模块
-
用于多层嵌套的复杂字典,根据key和下标来批量提取value
-
格式:
from jsonpath import jsonpath
jsonpath(a,'jsonpath语法规则字符串')
from jsonpath import 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
}
}
}
print(jsonpath(book_dict,'$..author'))
print(jsonpath(book_dict,'$..color'))
#以列表形式输出
#通过下标来获取所需的数据
4.xpath
- 节点选择语法:
- /:从根节点选取
- //:从当前节点选取(不考虑其位置)
(“/”隔开的是节点) - .:显示当前节点
- …:返回当前节点的父节点
- text():从开闭标签之间去文本内容
- @:从选取的节本标签中获取
- 节点修饰语法:
-
通过索引修饰节点 (即方括号[])
/html/body/div[3]/div/div[1]/div
/html/body/div[3]/div/div[1]/div[3]
/html/body/div[3]/div/div[1]/div[last()]: 最后一个索引
/html/body/div[3]/div/div[1]/div[last()-1]: 倒数第二个索引
/html/body/div[3]/div/div[1]/div[posation()>10] : 范围选择 -
通过属性值修饰节点:
//div[@id=“conntent-left”]/div/@id
出现在[ ]中的@:使用标签属性名和属性值修饰节点
出现在结尾的/@:取属性值 -
通过子节点的值修饰节点:
//span[i>2000]
//div[span[2]>=9.4] -
通过包含修饰:
//div[contains(@id,“qiushi_tag_”)]
//span[contains(texxt(),“一页”)]
- 选取未知节点语法
*:匹配任何元素节点
@*:匹配任何属性节点
node():匹配任何类型的节点
//h2/a|//td/a : xpath复合使用语法
5. lxml
- 创建element对象
将element对象转化为字符串
from lxml import etree
text = '''
<li>
<div class="item">
<div class="pic">
<em class="">1</em>
<a href="https://movie.douban.com/subject/1292052/">
<img width="100" alt="肖申克的救赎" src="https://img2.doubanio.com/view/photo/s_ratio_poster/public/p480747492.webp" class="">
</a>
</div>
<div class="info">
<div class="hd">
<a href="https://movie.douban.com/subject/1292052/" class="">
<span class="title">肖申克的救赎</span>
<span class="title"> / The Shawshank Redemption</span>
<span class="other"> / 月黑高飞(港) / 刺激1995(台)</span>
</a>
<span class="playable">[可播放]</span>
</div>
<div class="bd">
<p class="">
导演: 弗兰克·德拉邦特 Frank Darabont 主演: 蒂姆·罗宾斯 Tim Robbins /...<br>
1994 / 美国 / 犯罪 剧情
</p>
<div class="star">
<span class="rating5-t"></span>
<span class="rating_num" property="v:average">9.7</span>
<span property="v:best" content="10.0"></span>
<span>2324597人评价</span>
</div>
<p class="quote">
<span class="inq">希望让人自由。</span>
</p>
</div>
</div>
</div>
</li>
'''
html = etree.HTML(text)
#print(html) #输出:<Element html at 0x27a7fc7cd00>
#应用:
print(html.xpath('//div/@class'))
print(html.xpath('//span/text()'))
- etree的tostring方法可以将element转化为二进制类型。
故需要用encoding属性指定编码方法,否则可能会造成乱码。
** 且此方法会使原来不规则的html字符串补全为规则的html**
htmlstr = etree.tostring(html,encoding="utf-8").decode()
print(htmlstr)