前面我给大家介绍了正则和xpath,接下来我们在来看一个更简单的数据解析的模块Beautiful Soup
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据,官方解释如下:
- Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。
- Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。
- Beautiful Soup已成为和lxml一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。
一般对于这种官方的解释,我们大概看一眼就行。因为官方的解释一般都不说人话,对于零基础的小伙伴来说理解起来可能会很困难。
和lxml一样,BeautifulSoup
也是一个HTML/XML
的解析器,主要功能也是如何解析和提取HTML/XML
数据。
lxml只会局部遍历,而BeautifulSoup是基于HTML DOM的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会很大,所以性能要低于lxml BeautifulSoup用来解析HTML比较简单,支持CSS选择器,Python标准库中的HTML解析器,也支持lxml的XML解析器。
BeautifulSoup安装 pip install bs4
解析工具对比
抓取工具 | 速度 | 使用难度 |
---|---|---|
正则 | 最快 | 困难 |
lxml | 快 | 简单 |
BeautifulSoup | 慢 | 最简单 |
BeautifulSoup的基本使用
# 导入模块
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
# 创建Beautiful Soup对象
soup = BeautifulSoup(html_doc)
如果不传任何的解析器,会报错
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently
解析器我们可以传
我们一般会用lxml
,lxml
需要安装pip install lxml
soup = BeautifulSoup(html_doc, lxml)
接下来我们就可以用BeautifulSoup
的语法了
搜索文档树
Beautiful Soup
定义了很多搜索方法,这里着重介绍2个: find()
和 find_all()
from bs4 import BeautifulSoup
html_doc = """
<table class="tablelist" cellspacing="0" cellpadding="0">
<tbody>
<tr class="h">
<td class="1" width="374">职位名称</td>
<td>职位类别</td>
<td>人数</td>
<td>地点</td>
<td>发布时间</td>
</tr>
<tr class="even">
<td class="l"><a href="https://www.baidu.com">Python开发</a></td>
<td class="l">技术类</td>
<td class="l">1</td>
<td class="l">深圳</td>
<td class="l">2018-11-25</td>
</tr>
<tr>
<td><a href="https://www.qq.com">数据分析</a></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2018-11-24</td>
</tr>
<tr>
<td><a href="https://www.juran.com">机器学习</a></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2018-11-24</td>
</tr>
<tr>
<td><a href="https://www.python.com">爬虫工程师</a></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2018-11-24</td>
</tr>
<tr>
<td><a href="https://www.lg.com" id="test" class="test">NLP自然语言处理</a></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2018-11-24</td>
</tr>
</tbody>
</table>
如果我想获取所有的tr标签
soup = BeautifulSoup(html_doc, 'lxml')
trs = soup.find_all('tr')
print(trs) # 列表,用循环取值
获取第2个tr标签 limit取前2个标签,然后用下标取第二个
tr = soup.find_all('tr', limit=2)[1]
print(tr)
获取所有class等于even的tr标签
trs = soup.find_all('tr', {"class": "even"})
将所有id等于test,class也等于test的a标签提取出来
alist = soup.find_all('a', {"id": "test", "class": "test"})
获取所有的a标签的href属性
hrefs = soup.find_all('a')
for a in hrefs:
# 1.通过下表操作的方式
href = a['href']
获取所有的职位信息(纯文本)
# 过滤掉第一个
trs = soup.find_all('tr')[1:]
for tr in trs:
tds = tr.find_all("td")
title = tds[0]
# 获取到字符串 单独打印title是a标签
print(title.string)
find
和find_all
的区别
- find找到满足条件的第一个标签,只会返回一个元素
- find_all找到满足所有条件的标签,返回的是列表
CSS常用选择器介绍
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
# 选择p标签
p{
background-color:red;
}
# 根据class选择p标签
.line1{
background-color:pink;
}
#line3{
background-color:blue;
}
# div下面的p标签
.box p{
background-color:pink;
}
# 选择div class下面直接的p标签
.box > p {
background-color:pink;
}
# 选择name=username的input标签
input[name="username"]{
background-color:pink;
}
</style>
</head>
<body>
<div class="box">
<div>
<p>这是第零行</p>
</div>
<p class="line1">这是第一行</p>
<p class="line1">这是第二行</p>
<p id="line3">这是第三行</p>
</div>
<p>这是第四行</p>
<input type="text" name="username">
<input type="text" name="password">
</body>
</html>
- 通过标签名查找
print(soup.select('a'))
- 通过类名查找
print(soup.select('.sister')) 查找class=sister的标签
- 通过id查找
print(soup.select("#link1"))
-
组合查找
- 组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开:
print(soup.select("p #link1"))
直接子标签查找,则使用 > 分隔:
print(soup.select("head > title"))
- 通过属性查找
print(soup.select('a[href="http://example.com/elsie"]'))
-
获取内容
- 以上的 select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容。
遍历文档树
contents和children
tr = soup.find('tr')
# 返回所有子节点的列表 获取第一个tr下面的所有td
print(tr.contents)
# 返回所有子节点的迭代器 获取第一个tr下面的所有td
print(list(tr.children))
父节点
通过 .parent 属性来获取某个元素的父节点.
tdf = soup.find('td').parent
print(tdf)