当前位置:网站首页>14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
2022-06-25 06:38: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 总结

边栏推荐
- 弱大数定理的意义与证明
- [tool sharing] a software that pays equal attention to appearance and skills
- Finally, when you open source the applet ~
- 1W字|40 图|硬核 ES 实战
- 哇哦,好丰富呀。
- 单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
- Simple and complete steps of vivado project
- 48 张图 | 手摸手教你微服务的性能监控、压测和调优
- Ppt template of small fresh open class education courseware
- 【工具分享】一款颜值与技能并重的软件
猜你喜欢

1W words | 40 pictures | hard core es actual combat

弱大数定理的意义与证明

【LeetCode】two num·两数之和

终于等到你,小程序开源啦~

Expression of fatherly love

Efficient exploration | an application practice of ES geographical location query

レ / leilei

Capable people never complain about the environment!

有了 MySQL 为什么要用 NoSQL?

Simple and complete steps of vivado project
随机推荐
alphassl通配符证书送一个月
Orcad Schematic常用功能
Blue Bridge Cup SCM module code (external interrupt) (code + comment)
[Yu Yue education] engineering testing technology reference of Wenhua University
Error reported during vivado simulation common 17-39
lotus v1.16.0-rc2 Calibration-net
Can we use function pointers in go- Can we have function pointers in Go?
Reading sensor data with GPIO analog SPI interface
Qcom--lk phase I2C interface configuration scheme -i2c6
Chang Wei (variables and constants) is easy to understand
Change the current count of auto increment values in MySQL- Changing the current count of an Auto Increment value in MySQL?
Make fertilizer Safi from crop residues locally to increase yield by 30% and improve soil
What is the new business model of Taishan crowdfunding in 2022?
Blue Bridge Cup SCM module code (LED) (code + comments)
lotus v1.16.0-rc2 Calibration-net
Want to self-study SCM, do you have any books and boards worth recommending?
From perceptron to transformer, a brief history of deep learning
One year's time and University experience sharing with CSDN
Changing the background color of tab bar - changing the background color of tab bar
Your local changes to the following files would be overwritten by merge: . vs/slnx. sqlite