Python爬虫-BeautifulSoup解析

1.简介

BeautifulSoup 是一个用于解析 HTML 和 XML 文档的 Python 库。它提供了一种灵活且方便的方式来导航、搜索和修改树结构或标记文档。这个库非常适合网页抓取和数据提取任务,因为它允许你以非常直观的方式查询和操作文档内容。

2.安装 Beautiful Soup

终端输入:pip install beautifulsoup4

3.四个关键对象-覆盖了HTML或XML的所有内容

Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSoup , Comment .

3.1 BeatifulSoup对象

BeautifulSoup 对象在 BeautifulSoup 库中是一个特殊的对象,它代表了一个被解析的 HTML 或 XML 文档的整体内容。

我们可以使用BeautifulSoup方法实例化一个BeatifulSoup对象,接下来查看此对象的类型

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>
"""
from bs4 import BeautifulSoup
#这里,html_doc是你想要解析的HTML文档字符串,'html.parser'是解析器,它告诉BeautifulSoup使用Python的标准库来解析文档。
soup = BeautifulSoup(html_doc,'html.parser')
print(type(soup))

3.2 tag对象

tag对象与XML或HTML原生文档中的tag相同,我们可以使用BeautifulSoup对象来获取到tag对象。

通过tag对象获取属性值,方式:标签名['属性名'],示例如下:

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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,'html.parser')
tag=soup.p#获取tag对象,当有多个同名标签,这种获取方式只会获取第一个
print(type(tag))
print(tag['class'])#获取指定属性值

当然属性值可能会有多个,HTML 4定义了一系列可以包含多个值的属性.在HTML5中移除了一些,却增加更多.最常见的多值的属性是 class (一个tag可以有多个CSS的class). 还有一些属性 rel , rev , accept-charset , headers , accesskey . 在Beautiful Soup中多值属性的返回类型是list,如果某个属性看起来好像有多个值,但在任何版本的HTML定义中都没有被定义为多值属性,那么Beautiful Soup会将这个属性作为字符串返回,实例如下:

html_doc = """
<html><head><title>The Dormouse's story</title></head><body>
<p class="title test" id="title test"><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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,'html.parser')
tag=soup.p#获取tag对象
print(tag['class'])#获取指定属性值
print(tag['id'])

3.3 NavigableString对象

NavigableString 是 BeautifulSoup 库中的一个类,用于表示 HTML 或 XML 文档中的纯文本字符串,我们可以使用此对象获取标签中的值,获取方式为tag.string获取NavigableString对象,示例如下:

html_doc = """
<html><head><title>The Dormouse's story</title></head><body>
<p class="title test" id="title test"><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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,'html.parser')
tag=soup.p#获取tag对象
print(tag.string,type(tag.string))

3.4 Comment对象

 对象是一个特殊类型的 NavigableString 对象,他可用来表示注释内容

html_doc = """
<html><head><title>The Dormouse's story</title></head><body>
<p class="title test" id="title test"><!--<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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,'html.parser')
tag=soup.p#获取tag对象
print(tag.string,type(tag.string))

Beautiful Soup中定义的其它类型都可能会出现在XML的文档中: CData , ProcessingInstruction , Declaration , Doctype . Comment 对象类似,这些类都是 NavigableString 的子类,只是添加了一些额外的方法的字符串独享。

4.搜索文档树

搜索文档实际上是通过过滤器来实现的,这种过滤器类似于条件查询,过滤器可以被用在tag的name中,节点的属性中,字符串中或他们的混合中。

4.1 find_all方法

find_all方法法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件,当查询结果有多项时返回list列表。这是方法中的参数,下列是对这些参数的使用:

按属性查找
from bs4 import BeautifulSouphtml_doc = """  
<html><head><title>网站标题</title></head>  
<body>  
<p class="title"><b>第一个段落</b></p>  
<p class="story">第二个段落。</p>  
<p class="story">第三个段落。</p>  
<p class="title"><b>第四个段落</b></p>  
</body>  
</html>  
"""soup = BeautifulSoup(html_doc, 'html.parser')# 查找所有 class 属性为 "title" 的标签
title_tags = soup.find_all(attrs={"class": "title"})
for tag in title_tags:print(tag)


按CSS选择器查找

from bs4 import BeautifulSouphtml_doc = """  
<html><head><title>网站标题</title></head>  
<body>  
<p class="title"><b>第一个段落</b></p>  
<p class="story">第二个段落。</p>  
<p class="story">第三个段落。</p>  
<p class="title"><b>第四个段落</b></p>  
</body>  
</html>  
"""soup = BeautifulSoup(html_doc, 'html.parser')# 使用 CSS 类选择器查找
title_tags = soup.find_all(class_="title")
for tag in title_tags:print(tag)
# 使用 CSS 属性选择器查找
tags_with_href = soup.find_all(attrs={"href": True})
for tag in tags_with_href:print(tag)

按文本内容查找

你可以通过 string 参数来根据标签中的文本内容查找元素。

html_doc = """  
<html><head><title>网站标题</title></head>  
<body>  
<p class="title"><b>第一个段落</b></p>  
<p class="story">第二个段落。</p>  
<p class="story">第三个段落。</p>  
<p class="title"><b>第四个段落</b></p>  
</body>  
</html>  
"""soup = BeautifulSoup(html_doc, 'html.parser')
# 查找包含特定文本的 <p> 标签
p_tags_with_text = soup.find_all('p', string="第二个段落。")
for tag in p_tags_with_text:print(tag)

使用正则表达式查找

你还可以使用正则表达式来匹配标签中的文本内容。

from bs4 import BeautifulSoup
import re
html_doc = """  
<html><head><title>网站标题</title></head>  
<body>  
<p class="title"><b>第一个段落</b></p>  
<p class="story">第2个段落。</p>  
<p class="story">第三个段落。</p>  
<p class="title"><b>第四个段落</b></p>  
</body>  
</html>  
"""soup = BeautifulSoup(html_doc, 'html.parser')
p_tags_with_numbers = soup.find_all('p', string=re.compile(r'\d'))#\d表示匹配到任意数字,r表示普通字符串
for tag in p_tags_with_numbers:print(tag)

限制返回结果数量

你可以使用 limit 参数来限制 find_all 方法返回的结果数量。

from bs4 import BeautifulSoup
import re
html_doc = """  
<html><head><title>网站标题</title></head>  
<body>  
<p class="title"><b>第一个段落</b></p>  
<p class="story">第2个段落。</p>  
<p class="story">第三个段落。</p>  
<p class="title"><b>第四个段落</b></p>  
</body>  
</html>  
"""soup = BeautifulSoup(html_doc, 'html.parser')
# 只查找前两个 <p> 标签
first_two_p_tags = soup.find_all('p', limit=2)
for tag in first_two_p_tags:print(tag)
 

4.2 find方法

find方法与find_all方法使用方式基本一致,但是他只会匹配到第一项。

4.结尾

BeautifulSoup是解析爬取数据的利器,但是往往我们在采集数据时会遇到许多的问题,比如说ip封禁,明显是网站进行了反爬处理:限制IP请求频率。这个时候,代理ip解决这类问题就十分有效。这里推荐一款最近发现的代理商家:协采云IP池。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/661867.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java学习第02天-类型转换、运算符

目录 类型转换 自动类型转换 表达式的自动类型转换 强制类型转换 运算符 基本运算符 案例解答 连接字符串 自增自减运算符 面试习题 赋值运算符 比较运算符 逻辑运算符 基本逻辑运算符 短路逻辑运算符 三元运算符 基础知识 拓展案例 运算符优先级 读取用户…

DICOM 测试工具

一个DICOM测试工具。 引用了 fo-dicom 。fo-dicom 算是比较好用的&#xff0c;我的另外一个项目也是用了它。 using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; …

【C++STL详解(四)】--------vector的模拟实现

前言 还是那句话&#xff1a;模拟实现不是为了比库里面更好&#xff0c;而是去学习它的一些底层&#xff0c;能够让自己有更深的了解&#xff0c;为了更好的熟用&#xff01; vector底层图 由上图可以看出&#xff0c;它的底层实际上就是原生的指针T*&#xff0c;只不过会存在三…

可视化大屏也在卷组件化,组件绝对是效率利器呀。

组件化设计在B端上应用十分普遍&#xff0c;其实可视化大屏组件更为规范&#xff0c;本期分享组件化设计的好处&#xff0c;至于组件源文件如何获取&#xff0c;大家都懂的。 组件化设计对可视化大屏设计有以下几个方面的帮助&#xff1a; 提高可重用性&#xff1a; 组件化设…

SpringBoot指标监控

一.SpringBoot指标监控_添加Actuator功能 Spring Boot Actuator可以帮助程序员监控和管理SpringBoot应用&#xff0c;比如健康检查、内存使用情况统计、线程使用情况统计等。我 们在SpringBoot项目中添加Actuator功能&#xff0c;即可使用Actuator监控 项目&#xff0c;用法如…

启明云端2.4寸屏+ESP32-S3+小型智能调速电动家用除草机案例 触控三档调速,能显示电压故障码

今天给大家分享个启明云端2.4寸屏ESP32-S3小型智能调速电动家用除草机案例&#xff0c;国外有草坪文化&#xff0c;这个机器能智能触控三档调速&#xff0c;带屏能显示电压故障码&#xff0c;数显档位&#xff08;3档最大&#xff09;&#xff0c;触控屏&#xff0c;长按3秒就能…

C#技巧之窗体去鼠标化

简介 在窗体程序中不用鼠标&#xff0c;直接使用键盘完成想要的操作。 实现的方法有两种&#xff0c;一种是使用键盘上的Tab键使控件获得焦点&#xff0c;然后用enter键触发该控件上的事件&#xff08;一般为click事件&#xff09;。另一种是&#xff0c;为控件添加快捷键&am…

附录6-1 黑马优购项目-组件与过滤器

目录 1 过滤器-格式化价格 2 组件-搜索框 3 组件-数量框 4 组件-商品概况 4.1 格式化价格 4.2 选择性使用勾选框和数量框 4.3 源码 1 过滤器-格式化价格 这个项目中仅用到格式化价格这一种过滤器。过滤器文件位置为store/filter.wxs 文件内容是这样的&#xf…

APT预警攻击平台截获Nday

APT预警攻击平台截获Nday 2024年4月26日 设备漏洞【漏洞利用】H3C Magic R100任意代码执行漏洞(CVE-2022-34598) 0000 &#xff1a; 0010 &#xff1a; 0020 &#xff1a; 0030 &#xff1a; 0040 &#xff1a; 0050 &#xff1a; 0060 &#xff1a; 0070 &#xff1a;6F 72…

MySQL基础学习(待整理)

MySQL 简介 学习路径 MySQL 安装 卸载预安装的mariadb rpm -qa | grep mariadb rpm -e --nodeps mariadb-libs安装网络工具 yum -y install net-tools yum -y install libaio下载rpm-bundle.tar安装包&#xff0c;并解压&#xff0c;使用rpm进行安装 rpm -ivh \ mysql-communi…

【热闻速递】Google 裁撤 Python研发团队

&#x1f308;个人主页: 鑫宝Code &#x1f525;热门专栏: 闲话杂谈&#xff5c; 炫酷HTML | JavaScript基础 ​&#x1f4ab;个人格言: "如无必要&#xff0c;勿增实体" 文章目录 【&#x1f525;热闻速递】Google 裁撤 Python研发团队引入研究结论 【&#x1f5…

Liunx发布tomcat项目

Liunx在Tomcat发布JavaWeb项目 1.问题2.下载JDK3.下载Tomcat4.Tomcat本地JavaWeb项目打war包、解压、发布5.重启Tomcat,查看项目 1.问题 1.JDK 与 Tomcat 版本需匹配&#xff0c;否则页面不能正确显示 报错相关&#xff1a;Caused by: java.lang.ClassNotFoundException: java…