当前位置:网站首页>14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
2022-06-24 23:32:00 【安迪Python】
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
14.1 提取节点名称 属性 内容的方法
tag [tæɡ]:标签。
attr:属性。
string [strɪŋ]:字符串。

1. 获取节点名称
语法格式:bs对象.节点名称.name
返回的数据类型:字符串
from bs4 import BeautifulSoup
html_str = """<p align="center"><strong>应是绿肥红瘦。</strong></p>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# 获取p节点的名称
print(bs_duixiang.p.name)
print(type(bs_duixiang.p.name))
【终端输出】
p
<class 'str'>
运行代码后输出的p为节点名称,数据类型为字符串。
2. 获取节点属性
语法格式:bs对象.节点名称.attrs
返回的数据类型:字典
from bs4 import BeautifulSoup
html_str = """<p align="center"><strong>应是绿肥红瘦。</strong></p>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# 获取p节点的属性
print(bs_duixiang.p.attrs)
print(type(bs_duixiang.p.attrs))
【终端输出】
{'align': 'center'}
<class 'dict'>
运行代码后输出的align': 'center为节点属性,数据类型为字典。
align[əˈlaɪn]:对齐方式。
center[ˈsentə]:居中。
align表示代码属性名。
center表示属性值。
3. 获取节点内容
语法格式:bs对象.节点名称.string
返回的数据类型:可遍历的字符串对象。
from bs4 import BeautifulSoup
html_str = """<p align="center"><strong>应是绿肥红瘦。</strong></p>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# 获取p节点的内容
print(bs_duixiang.p.string)
print(type(bs_duixiang.p.string))
【终端输出】
应是绿肥红瘦。
<class 'bs4.element.NavigableString'>
14.2 实战练习
# 声明一个字符串变量,存储部分HTML代码
html_str = """
<div id="ArtContent">
<h1>古典诗词鉴赏之李清照篇——《如梦令》</h1>
</div>
<p align="center"><strong>昨夜雨疏风骤,</strong></p>
<p align="center"><strong>浓睡不消残酒,</strong></p>
<p align="center"><strong>试问卷帘人,</strong></p>
<p align="center"><strong>却道海棠依旧。</strong></p>
<p align="center"><strong>知否,</strong></p>
<p align="center"><strong>知否,</strong></p>
<p align="center"><strong>应是绿肥红瘦。</strong></p>
<a href="https://www.diyifanwen.com/m" target="_blank" class="print-link">
"""
# 步骤1:从bs4 库中导入BeautifulSoup类
from bs4 import BeautifulSoup
# 步骤2:传入参数,实例化BeautifulSoup类
# 参数1是要解析的HTML字符串
# 参数2是解析器(这里用lxml解析器)
# 实例化后得到一个BeautifulSoup对象
# bs_duixiang = <class 'bs4.BeautifulSoup'>
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("解析器解析后得到一个BeautifulSoup对象:")
print(type(bs_duixiang ),'\n')
# 步骤3:bs对象.tag名称获取tag对象
print("提取到的节点数据类型为tag对象:")
print("默认提取第一个p节点")
print(bs_duixiang.p,'\n')
# 步骤4:bs对象.节点名称.name提取节点标签名称
print("p节点的名称为:")
print(bs_duixiang.p.name,'\n')
# 步骤4:bs对象.节点名称.attrs提取节点标签属性
print("p节点的属性为:")
print(bs_duixiang.p.attrs,'\n')
# 步骤4:bs对象.节点名称.string提取节点标签内容
print("p节点的内容为:")
print(bs_duixiang.p.string,'\n')
print("name的数据类型为:",type(bs_duixiang.p.name))
print("attrs的数据类型为:",type(bs_duixiang.p.attrs))
print("string的数据类型为:",type(bs_duixiang.p.string))
【终端输出】
解析器解析后得到一个BeautifulSoup对象:
<class 'bs4.BeautifulSoup'>
提取到的节点数据类型为tag对象:
默认提取第一个p节点
<p align="center"><strong>昨夜雨疏风骤,</strong></p>
p节点的名称为:
p
p节点的属性为:
{'align': 'center'}
p节点的内容为:
昨夜雨疏风骤,
name的数据类型为: <class 'str'>
attrs的数据类型为: <class 'dict'>
string的数据类型为: <class 'bs4.element.NavigableString'>
14.3 总结

边栏推荐
- centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
- LINQ query (3)
- F - Spices(线性基)
- [day 26] given the ascending array nums of n elements, find a function to find the subscript of target in nums | learn binary search
- PyTorch学习笔记(七)------------------ Vision Transformer
- ida中交叉引用的解析
- Leecode learning notes - the shortest path for a robot to reach its destination
- 3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
- How to monitor the log through the easycvr interface to observe the platform streaming?
- E - average and median
猜你喜欢

ProcessOn制作ER过程(自定义)

leecode学习笔记-机器人走到终点的最短路径

Processon producer process (customized)

random list随机生成不重复数

Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career

Is it out of reach to enter Ali as a tester? Here may be the answer you want

数据库系统概论必背知识
![[STL source code analysis] configurator (to be supplemented)](/img/87/0ed1895e9cdb5327411c0c9cb0197f.png)
[STL source code analysis] configurator (to be supplemented)

Software testing salary in first tier cities - are you dragging your feet

After reciting the eight part essay, I won the hemp in June
随机推荐
文件系统 -- 磁盘基础知识和FAT32文件系统详细介绍
AI服装生成,帮你完成服装设计的最后一步
|How to analyze bugs? Professional summary and analysis
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(1)——迁移数据到节点1
How to uninstall CUDA
F - Spices(线性基)
PE文件基础结构梳理
npm包发布详细教程
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
Unity archive system - file in JSON format
Intranet learning notes (6)
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (3) -- set the database to archive mode
Once beego failed to find bee after passing the go get command Exe's pit
The ecosystem of the yuan universe
random list随机生成不重复数
Leecode learning notes - the shortest path for a robot to reach its destination
指南针靠谱吗?开证券账户安全吗?
leecode学习笔记-机器人走到终点的最短路径
Unity存档系统——Json格式的文件
Transformers Roberta如何添加tokens