当前位置:网站首页>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 总结

边栏推荐
- Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]
- 3632. Binary sum
- lotus v1.16.0-rc2 Calibration-net
- 鸿蒙页面菜单的选择
- 有了 MySQL 为什么要用 NoSQL?
- 基于 KubeSphere 的分级管理实践
- Lotus v1.16.0-rc2 Calibration net
- The process of making wooden barrels with 3DMAX software: a three-step process
- Unity get resource path
- Finally, when you open source the applet ~
猜你喜欢

The king scheme in distributed locks - redisson

Esp8266 & sg90 steering gear & Lighting Technology & Arduino

What is the new business model of Taishan crowdfunding in 2022?

レ / leilei

弱大数定理的意义与证明

48 张图 | 手摸手教你微服务的性能监控、压测和调优

Omni toolbox direct download

Uncaught TypeError: Cannot read properties of undefined (reading ‘prototype‘)

Ppt template of small fresh open class education courseware

我们不一样
随机推荐
MySQL (12) -- Notes on changing tables
joda. Time get date summary
[C language] add separator to string
Conditional grouping with $exists inside $cond
313. Binary sum
Sqlmap advanced use – cookies
Change the current count of auto increment values in MySQL- Changing the current count of an Auto Increment value in MySQL?
终于等到你,小程序开源啦~
想买股票去哪个证券公司开户更快更安全
Keil debug view variable prompt not in scope
Blue Bridge Cup SCM module code (matrix key) (code + comments)
Following the last minor upgrade of nodemcu (esp8266)
我们不一样
アルマ / alchemy girl
深入解析 Apache BookKeeper 系列:第三篇——读取原理
100 times larger than the Milky way, Dutch astronomers found mysterious objects in deep space
I have used it for six years!
How to get the difference between two dates rounded to hours
1W字|40 图|硬核 ES 实战
SQL query, if value is null then return 1 - SQL query, if value is null then return 1