BeautifulSoup解析库的介绍和使用
### BeautifulSoup解析库的介绍和使用
### 三大选择器:节点选择器,方法选择器,CSS选择器
### 使用建议:方法选择器 > CSS选择器 > 节点选择器 ## 测试文本
text = '''
<html><head><title>there is money</title></head>
<body>
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a href="https://www.baidu.com/1" class="error" id="l1"><span><!-- 1 --></span></a>,
<a href="https://www.baidu.com/2" class="error" id="l2"><span>2</span></a> and
<a href="https://www.baidu.com/3" class="error" id="l3">3</a>;
66666666666
</p>
<p class='body'>...</p>
'''
1. 基本用法
## 基本用法
from bs4 import BeautifulSoup # 初始化BeautifulSoup对象,选择lxml类型
soup = BeautifulSoup(text, 'lxml')
# 以标准的缩进格式输出
print(soup.prettify())
# 提取title节点的文本内容
print(soup.title.string) '''
输出内容:
<html>
<head>
<title>
there is money
</title>
</head>
<body>
<p class="title" name="dmr">
<b>
there is money
</b>
</p>
<p class="money">
good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1">
<!-- 1 -->
</a>
,
<a class="error" href="https://www.baidu.com/2" id="l2">
2
</a>
and
<a class="error" href="https://www.baidu.com/3" id="l3">
3
</a>
;
66666666666
</p>
<p class="body">
...
</p>
</body>
</html>
there is money
'''
2. 节点选择器
### 节点选择器
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
print(type(soup))
print(soup.title)
print(type(soup.title))
print(soup.p)
print(soup.head) '''
输出结果:
<class 'bs4.BeautifulSoup'>
<title>there is money</title>
<class 'bs4.element.Tag'>
<p class="title" name="dmr"><b>there is money</b></p>
<head><title>there is money</title></head>
''' ## 提取信息
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
# 提取title标签的文本内容
print(soup.title.string)
# p表情的名称
print(soup.p.name)
# p标签的属性,字典格式
print(soup.p.attrs)
print(soup.p.attrs.get('name'))
# attrs可省略,直接以字典的提取方式进行信息提取
print(soup.p['class'])
print(soup.p.get('class'))
print(soup.p.string) '''
输出内容:
there is money
p
{'class': ['title'], 'name': 'dmr'}
dmr
['title']
['title']
there is money
''' ## 嵌套选择,套中套 from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
print(soup.body.p.string) '''
输出内容:
there is money
''' ## 关联选择
## 子节点和子孙节点
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
# 直接子节点,包含换行符文本内容等;contents获取到一个list, children生成一个迭代器(建议使用)
print(soup.body.contents)
print(len(soup.body.contents))
print(soup.body.children)
for i, child in enumerate(soup.body.children):
print(i, child)
print(soup.body.descendants)
for j, item in enumerate(soup.body.descendants):
print(j, item) '''
输出结果:
['\n', <p class="title" name="dmr"><b>there is money</b></p>, '\n', <p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>, '\n', <p class="body">...</p>, '\n']
7
<list_iterator object at 0x0000000002DAD320>
0 1 <p class="title" name="dmr"><b>there is money</b></p>
2 3 <p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
4 5 <p class="body">...</p>
6 <generator object Tag.descendants at 0x0000000002D67E58>
0 1 <p class="title" name="dmr"><b>there is money</b></p>
2 <b>there is money</b>
3 there is money
4 5 <p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
6 good good study, day day up 7 <a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>
8 <span><!-- 1 --></span>
9 1
10 , 11 <a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a>
12 <span>2</span>
13 2
14 and 15 <a class="error" href="https://www.baidu.com/3" id="l3">3</a>
16 3
17 ;
66666666666 18 19 <p class="body">...</p>
20 ...
21
''' ## 父节点和祖先节点
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
print(soup.a.parent)
print(soup.a.parents)
for i, parent in enumerate(soup.a.parents):
print(i, parent) '''
输出结果:
<p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
<generator object PageElement.parents at 0x0000000002D68E58>
0 <p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
1 <body>
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
<p class="body">...</p>
</body>
2 <html><head><title>there is money</title></head>
<body>
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
<p class="body">...</p>
</body></html>
3 <html><head><title>there is money</title></head>
<body>
<p class="title" name="dmr"><b>there is money</b></p>
<p class="money">good good study, day day up
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>,
<a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a> and
<a class="error" href="https://www.baidu.com/3" id="l3">3</a>;
66666666666
</p>
<p class="body">...</p>
</body></html>
''' ## 兄弟节点
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
print('Next sibling: ', soup.a.next_sibling)
print('Previous sibling: ', soup.a.previous_sibling)
print('Next siblings: ', soup.a.next_siblings)
print('Previous siblings: ', soup.a.previous_sibling) '''
输出结果:
Next sibling: , Previous sibling: good good study, day day up Next siblings: <generator object PageElement.next_siblings at 0x0000000002D67E58>
Previous siblings: good good study, day day up
'''
3. 方法选择器
### 方法选择器,较为灵活
## find_all方法,查询所有符合条件的,返回一个列表,元素类型为tag
## find方法,查询符合条件的第一个元素,返回一个tag类型对象
## 同理,find_parents和find_parent
## find_next_siblings和find_next_sibling
## find_previous_siblings和find_previous_sibling
## find_all_next和find_next
## find_all_previous和find_previous
from bs4 import BeautifulSoup
import re soup = BeautifulSoup(text, 'lxml')
# 找到节点名为a的节点,为一个列表
print(soup.find_all(name='a'))
print(soup.find_all(name='a')[0])
# 找到id属性为l1, class属性为error的节点
print(soup.find_all(attrs={'id': 'l1'}))
print(soup.find_all(class_='error'))
# 通过文本关键字来进行匹配文本内容
print(soup.find_all(text=re.compile('money'))) '''
输出内容:
[<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>, <a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a>, <a class="error" href="https://www.baidu.com/3" id="l3">3</a>]
<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>
[<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>]
[<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>, <a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a>, <a class="error" href="https://www.baidu.com/3" id="l3">3</a>]
['there is money', 'there is money']
'''
4. CSS选择器
### CSS选择器,select方法,返回一个列表
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
print(soup.select('p a'))
print(soup.select('.error'))
print(soup.select('#l1 span'))
print(soup.select('a'))
print(type(soup.select('a'))) '''
输出内容:
[<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>, <a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a>, <a class="error" href="https://www.baidu.com/3" id="l3">3</a>]
[<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>, <a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a>, <a class="error" href="https://www.baidu.com/3" id="l3">3</a>]
[<span><!-- 1 --></span>]
[<a class="error" href="https://www.baidu.com/1" id="l1"><span><!-- 1 --></span></a>, <a class="error" href="https://www.baidu.com/2" id="l2"><span>2</span></a>, <a class="error" href="https://www.baidu.com/3" id="l3">3</a>]
<class 'bs4.element.ResultSet'>
''' ## 嵌套选择,获取属性,获取文本
from bs4 import BeautifulSoup soup = BeautifulSoup(text, 'lxml')
# 嵌套选择
for i in soup.select('a'):
print(i.select('span'))
# 获取属性
print(soup.select('a')[0].attrs)
print(soup.select('a')[0].get('class'))
# 获取文本
print(soup.select('a')[1].string)
print(soup.select('a')[2].get_text()) '''
输出结果:
[<span><!-- 1 --></span>]
[<span>2</span>]
[]
{'href': 'https://www.baidu.com/1', 'class': ['error'], 'id': 'l1'}
['error']
2
3
'''
BeautifulSoup解析库的介绍和使用的更多相关文章
- BeautifulSoup解析库
解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(html, 'html.parser') 速度适中,容错能力强 老版本python容错能力差 lxml HTML解 ...
- 第三节:Web爬虫之BeautifulSoup解析库
Beautiful Soup官方说明: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为 ...
- pyquery解析库的介绍和使用
### pyquery的介绍和使用 ## 测试文本 text = ''' <html><head><title>there is money</title&g ...
- BeautifulSoup与Xpath解析库总结
一.BeautifulSoup解析库 1.快速开始 html_doc = """ <html><head><title>The Dor ...
- xpath beautiful pyquery三种解析库
这两天看了一下python常用的三种解析库,写篇随笔,整理一下思路.太菜了,若有错误的地方,欢迎大家随时指正.......(conme on.......) 爬取网页数据一般会经过 获取信息-> ...
- Python爬虫3大解析库使用导航
1. Xpath解析库 2. BeautifulSoup解析库 3. PyQuery解析库
- 爬虫模块介绍--Beautifulsoup (解析库模块,正则)
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...
- 爬虫 解析库re,Beautifulsoup,
re模块 点我回顾 Beautifulsoup模块 #安装 Beautiful Soup pip install beautifulsoup4 #安装解析器 Beautiful Soup支持Pytho ...
- 解析库之re,Beautifulsoup
本篇导航: 介绍 基本使用 遍历文档树 搜索文档树 总结 re模块在之前的python进阶中有讲过不再做过多的阐述,本篇为BeautifulSoup库的分析 20.collections模块和 ...
随机推荐
- FastAPI 学习之路(三十四)数据库多表操作
之前我们分享的是基于单个的数据库表的操作,我们在设计数据库的时候也设计了跨表,我们可以看下数据库的设计. class User(Base): __tablename__ = "users&q ...
- 用建造者模式实现一个防SQL注入的ORM框架
本文节选自<设计模式就该这样学> 1 建造者模式的链式写法 以构建一门课程为例,一个完整的课程由PPT课件.回放视频.课堂笔记.课后作业组成,但是这些内容的设置顺序可以随意调整,我们用建造 ...
- tar 解压分割压缩文件
被分割后的压缩文件必须先合并成一个压缩文件才能正常的解压. 第一步.合并压缩文件 第二步.正常解压 $ls TINA-1.3.tar.gzaa TINA-1.3.tar.gzab TINA-1.3.t ...
- 绑定socket描述符到一个网络设备
网络编程中有时明明用eth0的地址来bind一个udp套接口, 可是发出去的包却是从eht1走的, 在网上找到这么一段话解释该问题: 在多 IP/网卡主机上,UDP ...
- hdu 2571 命运(水DP)
题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j), ...
- SSH 提示密码过期,如何通过 ansible 批量更新线上服务器密码
起因 线上环境是在内网,登陆线上环境需要使用 VPN + 堡垒机 登陆,但是我日常登陆线上环境都是 VPN + 堡垒机 + Socks5常驻代理,在shell端只需要保存会话,会话使用socks5代理 ...
- 基于Dapr的 Azure 容器应用
微软在 Ignite 2021 大会上发布了预览版的Azure Container Apps,这是一个完全托管的无服务器容器运行时间,用于大规模构建和运行现代应用程序.从2021 年 11 月 2 日 ...
- Laravel/Lumen 分组求和问题 where groupBy sum
在Laravel中使用分组求和,如果直接使用Laravel各数据库操作方法,应该会得出来如下代码式: DB::table('table_a') ->where('a','=',1) ->g ...
- Spark面试题(四)
1.Spark中的HashShufle的有哪些不足? 1)shuffle产生海量的小文件在磁盘上,此时会产生大量耗时的.低效的IO操作: 2)容易导致内存不够用,由于内存需要保存海量的文件操作句柄和临 ...
- maven项目中 把依赖包打进jar包
在pom.xml文件中增加build配置 1 <build> 2 <plugins> 3 <plugin> 4 <artifactId>maven-as ...