安装
pip intsall bs4
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代码不完整的时候,我们这时候就会需要使用Beautiful, Beautiful会自动帮我们补全html的代码所缺失的结构
prettify
: 将html代码进行漂亮的打印
# -*- coding: utf-8 -*-
# @Time : 2020/2/5 12:37
# @Author : 大数据小J
# 导入
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>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
运行结果:
<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 class="sister" href="http://example.com/elsie" id="link1">
Elsie
</a>
,
<a class="sister" href="http://example.com/lacie" id="link2">
Lacie
</a>
and
<a class="sister" href="http://example.com/tillie" id="link3">
Tillie
</a>
;
and they lived at the bottom of a well.
</p>
<p class="story">
...
</p>
</body>
</html>
BeautifulSoup
find
: 查找的是第一个标签
attrs={}
:里面传入的字典,属性对应值
find_all
: 查找的所有的标签
attrs={}
:里面传入的字典,属性对应值
string
: 返回一个子节点内容
strings
: 返回多条数据的内容(但包括换行符以及数据)
stripped_strings
: 返回多条数据内容(返回的结果为纯文本数据)
# -*- coding: utf-8 -*-
# @Time : 2020/2/4 22:23
# @Author : 大数据小J
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">区块链高级研发工程师</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.result.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">高级业务运维工程师</a></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2018-11-24</td>
</tr>
</tbody>
</table>
"""
soup = BeautifulSoup(html_doc, 'lxml')
# 1.获取所有tr标签
data = soup.find_all('tr') # 查找所有的tr标签
# 2.获取第2个tr标签
data2 = soup.find_all('tr')[1]
# 3.获取所有class等于even的tr标签
data3 = soup.find_all('tr', attrs={'class': 'even'})
# 4.获取所有的a标签的href属性
data4s = soup.find_all('a')[1:]
for data4 in data4s:
print(data4.get('href'))
# 5.获取所有的职位信息(纯文本)
data5s = soup.find_all('tr')
for data5 in data5s[1:]:
print(list(data5.strings))
BeautifulSoup4四大对象种类
BeautifulSoup4将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:
- Tag
- NavigableString
- BeautifulSoup
- Comment
遍历文档树
contents和children
- contents返回所有子节点的列表
- children返回所有子节点的迭代器