bs4上
1. bs4快速入门
1.1 基本概念
Beautiful Soup 是⼀个可以从HTML或XML文件中提取数据的网页信息提取库。
Beautiful Soup 中文文档链接:
https://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
ba4与XPath相比,最大的优势在于不用记一些语法,bs4使用时直接通过对象调用方法就可以查找我们所需要的数据。
1.2 简单代码示例
下面的一段HTML代码将作为例子被多次用到,这是爱丽丝梦游仙境的一段内容(以后内容中简称为爱丽丝文档):
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>
"""
现在尝试用BeautifulSoup
解析上述文本,得到一个BeautifulSoup
的对象。
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, features='lxml')
print(soup)
代码中的features='lxml'
表示指定文本的解析方式。关于解析器,这里不再详细展开,官方文档推荐使用lxml,具体内容有需要的读者可以去笔者给的官方文档中查询。
运行结果:
修改代码,让其按照标准的缩进格式的结构输出:
print(soup.prettify())
运行结果:
紧接上述代码:
print('soup.title:', soup.title)
print('soup.title.name:', soup.title.name)
print('soup.title.string:', soup.title.string)
# 只打印找到的第一个p标签
print('soup.p:', soup.p)
# 打印找到的所有的p标签
p_r = soup.find_all('p')
print(p_r, len(p_r))
links = soup.find_all('a')
# 遍历所有的a标签,并打印它的href属性
for link in links:
print(link.get('href'))
运行结果:
通过上述例子可以感受到,bs4模块使用时不需要记住语法,只需要会调用相应的方法就能找到想要的数据。
2. bs4对象种类
对象种类 | 含义 |
---|---|
Tag | 标签 |
NavigableString | 可导航的字符串 |
BeautifulSoup | 模块基本类 |
Comment | 注释 |
示例代码:
代码中的soup
依旧是加载爱丽丝文档创建的BeautifulSoup
对象。
print('title标签的类型:', type(soup.title))
print('a标签的类型:', type(soup.a))
print('p标签的类型:', type(soup.p))
print('p标签的名字:', soup.p.name)
print('p标签的属性:', soup.p.attrs)
print('p标签的文本内容:', soup.p.string)
print('p标签的文本内容的类型:', type(soup.p.string))
html_doc2 = '<a><!--圣诞节快乐!!--></a>'
soup2 = BeautifulSoup(html_doc2, 'lxml')
print(soup2.a.string)
print(type(soup2.a.string))
运行结果:
代码中的soup
变量就是一个BeautifulSoup
对象,查看bs4模块的源码:
BeautifulSoup
继承于Tag
,说明Tag
的功能,BeautifulSoup
对象全部都能实现。
3. 遍历文档树
3.1 contents、children和descendants
contents:返回的是一个所有子节点的列表;
children:返回的是一个子节点的迭代器;
descendants:返回的是一个生成器,遍历所有子节点中的内容。
示例代码:
代码中的soup
依旧是加载爱丽丝文档创建的BeautifulSoup
对象。
# 获取html标签
html_tag = soup.html
# contents返回所有子节点的列表
print(html_tag.contents, type(html_tag.contents))
print('-'*60)
# children返回子节点的迭代器
print(html_tag.children)
for i in html_tag.children:
print(i)
print('-'*60)
# descendants返回的是一个生成器,可一层层遍历子节点
head_tag = soup.head
print(head_tag.descendants)
for i in head_tag.descendants:
print(i)
运行结果:
3.2 string、strings和stripped_strings
string:获取标签里面的内容;
strings:返回是一个生成器对象用过来获取多个标签内容;
stripped_strings:和strings基本一致 但是它可以把多余的空格去掉。
示例代码:
代码中的soup
依旧是加载爱丽丝文档创建的BeautifulSoup
对象。
html_tag = soup.html
# 尝试打印html标签中的内容
print(html_tag.string)
# 打印html中所有子节点的内容
print(html_tag.strings)
for cont in html_tag.strings:
print(cont)
print('-'*60)
print(html_tag.stripped_strings)
for cont in html_tag.stripped_strings:
print(cont)
运行结果:
3.3 parent和parents
parent直接获得父节点;
parents获取所有的父节点。
示例代码:
代码中的soup
依旧是加载爱丽丝文档创建的BeautifulSoup
对象。
title_tag = soup.title
# 打印title标签的父节点
print(title_tag.parent)
print('-'*60)
print(soup.html.parent)
print('-'*60)
a_tag = soup.a
print(a_tag.parents)
# 遍历a标签的所有父节点
for a_p in a_tag.parents:
print(a_p)
运行结果:
类似于descendants返回的生成器,parents也会一层层向上遍历自己所有的父节点。
3.4 next_sibling/s、previous_sibling/s
next_sibling 下一个兄弟结点
previous_sibling 上一个兄弟结点
next_siblings 下一个所有兄弟结点
previous_siblings上一个所有兄弟结点
示例代码:
代码中的soup
依旧是加载爱丽丝文档创建的BeautifulSoup
对象。
from bs4 import BeautifulSoup
# 换一个更为简洁的html文本作为演示
html_tex = '<a><b>bbb</b><c>ccc</c></a>'
soup2 = BeautifulSoup(html_tex,'lxml')
print(soup2.prettify())
print('-'*60)
b_tag = soup2.b
# 打印b标签的下一个兄弟节点
print(b_tag.next_sibling)
print('-'*60)
c_tag = soup2.c
# 打印c标签的上一个兄弟节点
print(c_tag.previous_sibling)
运行结果:
4. find()和find_all()方法的使用
find_all() 方法以列表形式返回所有的搜索到的标签数据;
find() 方法返回搜索到的第⼀条数据。
find_all() 参数:
def find_all(self, name=None, attrs={}, recursive=True, text=None,
limit=None, **kwargs):
name : tag名称
attr : 标签的属性
recursive : 是否递归搜索
text : ⽂本内容
limit : 限制返回条数
kwargs : 关键字参数
查找数据时用的比较多的是字符串过滤器和列表过滤器,经常和find_all()搭配使用。
4.1 案例演示一
依旧先通过加载爱丽丝文档演示find()和findall()方法的使用:
# 查找第一个a标签,'a'属于字符串过滤器
a_tag = soup.find('a')
print(a_tag)
# 查找所有的a标签,以列表形式返回
a_tags = soup.find_all('a')
print(a_tags)
# 查找所有的title和吧标签,['title', 'b']属于列表过滤器
print(soup.find_all(['title', 'b']))
运行结果:
4.2 案例演示二
接下来的演示中都将使用如下html文本:
html_text = """
<table class="tablelist" cellpadding="0" cellspacing="0">
<tbody>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=33824&keywords=python&tid=87&lid=2218">22989-金融云区块链高级研发工程师(深圳)</a></td>
<td>技术类</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-25</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=29938&keywords=python&tid=87&lid=2218">22989-金融云高级后台开发</a></td>
<td>技术类</td>
<td>2</td>
<td>深圳</td>
<td>2017-11-25</td>
</tr>
<tr class="odd">
<td class="l square"><a id="test" class="test" target='_blank' href="position_detail.php?id=34511&keywords=python&tid=87&lid=2218">SNG11-高级业务运维工程师(深圳)</a></td>
<td>技术类</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
</tbody>
</table>
"""
- 获取所有的tr标签
# find_all = findAll
trs = soup.find_all('tr')
for tr in trs:
print(tr)
print('-'*60)
- 获取第二个tr标签
# limit=2, 限制查找个数为2
tr = soup.find_all('tr', limit=2)[1]
print(tr)
- 获取所有class等于odd的tr标签 class_ 表示就是这个class
trs = soup.find_all('tr', class_='odd')
# 也可以按照如下方式书写:
# trs = soup.find_all('tr', attrs={'class':'odd'})
for tr in trs:
print(tr)
print('-' * 60)
- 将所有id等于test,class也等于test的a标签提取出来
a_tags = soup.find_all('a', attrs={'id': 'test', 'class': 'test'})
for a in a_tags:
print(a)
- 获取所有a标签的href属性
a_tags = soup.find_all('a')
for a in a_tags:
href = a.get('href')
# 相同效果的写法
# href = a['href']
print(href)
- 获取所有职位信息(文本数据)
trs = soup.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
# 查找每个tr标签中的第一个td标签的内容
job_name = tds[0].string
print(job_name)