当前位置:网站首页> 18. `bs对象.节点名.next_sibling` 获取兄弟节点
18. `bs对象.节点名.next_sibling` 获取兄弟节点
2022-06-29 02:31:00 【安迪Python】
18. bs对象.节点名.next_sibling previous_sibling 获取兄弟节点
1. bs对象.节点名.next_sibling 获取后一个兄弟节点

sibling [ˈsɪblɪŋ]:兄弟。
语法格式:bs对象.节点名.next_sibling
返回值:节点对象。
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """
<html>
<head><meta charset="utf-8"><title>古诗2首</title></head>
<body>
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
<div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3>
<p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p>
</div>
</body>
</html>
"""
# 1. 从bs4解析库导入BeautifulSoup类用于解析数据
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入要解析的字符串html_str和解析器lxml,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名p——获取第1个p节点:")
print(bs_duixiang.p,'\n')
print("步骤2.1:bs对象.节点名p.next_sibling——获取第2个p节点:")
print(bs_duixiang.p.next_sibling,'\n')
print("步骤2.2:获取到的数据类型为tag对象:")
print(type(bs_duixiang.p.next_sibling),'\n')
【终端输出】
步骤1:bs对象.节点名p——获取第1个p节点:
<p>试问卷帘人</p>
步骤2.1:bs对象.节点名p.next_sibling——获取第2个p节点:
<p>却道海棠依旧</p>
步骤2.2:获取到的数据类型为tag对象:
<class 'bs4.element.Tag'>
2. bs对象.节点名.next_siblings 获取后面的所有兄弟节点
语法格式:bs对象.节点名.next_siblings
返回值:生成器。
取值方法:
方法1:for循环从生成器中取值for i,child in enumerate(bs_duixiang.p.next_siblings)
方法2:用list函数从生成器中取值list(enumerate(bs_duixiang.p.next_siblings))
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """
<html>
<head><meta charset="utf-8"><title>古诗2首</title></head>
<body>
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
<div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3>
<p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p>
</div>
</body>
</html>
"""
# 1. 从bs4解析库导入BeautifulSoup类用于解析数据
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入要解析的字符串html_str和解析器lxml,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名p——获取第1个p节点:")
print(bs_duixiang.p,'\n')
print("步骤2.1:bs对象.节点名p.next_siblings——获取第1个p节点后面的所有兄弟节点:")
print(bs_duixiang.p.next_siblings,'\n')
print("步骤2.2:获取到的数据类型为生成器:")
print(type(bs_duixiang.p.next_siblings),'\n')
print("取值方法1:利用for循环从生成器中取值,i表示元素序号,child表示元素:")
for i, child in enumerate(bs_duixiang.p.next_siblings) :
print(i, child)
print('\n')
print("取值方法2:用list函数从生成器中取值,得到的是列表:")
print(list(enumerate(bs_duixiang.p.next_siblings)))
【终端输出】
步骤1:bs对象.节点名p——获取第1个p节点:
<p>试问卷帘人</p>
步骤2.1:bs对象.节点名p.next_siblings——获取第1个p节点后面的所有兄弟节点:
<generator object PageElement.next_siblings at 0x000001C8C4BD46D0>
步骤2.2:获取到的数据类型为生成器:
<class 'generator'>
取值方法1:利用for循环从生成器中取值,i表示元素序号,child表示元素:
0 <p>却道海棠依旧</p>
1 <p>知否?知否?</p>
2 <p>应是绿肥红瘦</p>
取值方法2:用list函数从生成器中取值,得到的是列表:
[(0, <p>却道海棠依旧</p>), (1, <p>知否?知否?</p>), (2, <p>应是绿肥红瘦</p>)]
3. bs对象.节点名.previous_sibling 获取前一个兄弟节点
previous [ˈpriːviəs]:前面的。
语法格式:bs对象.节点名.previous_sibling
返回值:节点对象。
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """
<html>
<head><meta charset="utf-8"><title>古诗2首</title></head>
<body>
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
<div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3>
<p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p>
</div>
</body>
</html>
"""
# 1. 从bs4解析库导入BeautifulSoup类用于解析数据
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入要解析的字符串html_str和解析器lxml,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名p——获取第1个p节点:")
print(bs_duixiang.p,'\n')
print("步骤2.1:bs对象.节点名.previous_siblingg——获取第p节点前面的h3节点:")
print(bs_duixiang.p.previous_sibling,'\n')
print("步骤2.2:获取到的数据类型为tag对象:")
print(type(bs_duixiang.p.previous_sibling),'\n')
【终端输出】
步骤1:bs对象.节点名p——获取第1个p节点:
<p>试问卷帘人</p>
步骤2.1:bs对象.节点名.previous_siblingg——获取第p节点前面的h3节点:
<h3>李清照<span>(宋)</span></h3>
步骤2.2:获取到的数据类型为tag对象:
<class 'bs4.element.Tag'>
4. bs对象.节点名.previous_siblings 获取前面的所有兄弟节点
语法格式:bs对象.节点名.previous_siblings
返回值:生成器。
取值方法:
方法1:for循环从生成器中取值for i,child in enumerate(bs_duixiang.p.previous_siblings)
方法2:用list函数从生成器中取值list(enumerate(bs_duixiang.p.previous_siblings))
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """
<html>
<head><meta charset="utf-8"><title>古诗2首</title></head>
<body>
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
<div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3>
<p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p>
</div>
</body>
</html>
"""
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入要解析的字符串html_str和解析器lxml,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名p——获取第1个p节点:")
print(bs_duixiang.p,'\n')
print("步骤2.1:bs对象.节点名p.previous_siblings——获取第1个p节点前面的所有兄弟节点:")
print(bs_duixiang.p.previous_siblings,'\n')
print("步骤2.2:获取到的数据类型为生成器:")
print(type(bs_duixiang.p.previous_siblings),'\n')
print("取值方法1:利用for循环从生成器中取值,i表示元素序号,child表示元素:")
for i,child in enumerate(bs_duixiang.p.previous_siblings) :
print(i, child)
print('\n')
print("取值方法2:用list函数从生成器中取值,得到的是列表:")
print(list(enumerate(bs_duixiang.p.previous_siblings)))
【终端输出】
步骤1:bs对象.节点名p——获取第1个p节点:
<p>试问卷帘人</p>
步骤2.1:bs对象.节点名p.previous_siblings——获取第1个p节点前面的所有兄弟节点:
<generator object PageElement.previous_siblings at 0x000001C8C4B05900>
步骤2.2:获取到的数据类型为生成器:
<class 'generator'>
取值方法1:利用for循环从生成器中取值,i表示元素序号,child表示元素:
0 <h3>李清照<span>(宋)</span></h3>
1 <h2>如梦令</h2>
取值方法2:用list函数从生成器中取值,得到的是列表:
[(0, <h3>李清照<span>(宋)</span></h3>), (1, <h2>如梦令</h2>)]
5. 总结

边栏推荐
- Set set
- Application of fsockopen function
- Prepare for the Blue Bridge Cup - double pointer, BFS
- 11 go Foundation: Interface
- 字符串属性练习
- OpenResty 使用介绍
- Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
- MySQL binlog log cleanup
- LinkedList学习
- QT basics tutorial: data types and containers
猜你喜欢

Redis master-slave replication

對補wasm環境的一些測試

Chrome browser close update Popup

学习太极创客 — MQTT 第二章(九)本章测试

Have you learned the common SQL interview questions on the short video platform?

Deploy redis high availability cluster

How to use project Gantt chart to make project report

To apply for a test engineer after years, the resume with high scores should be written like this

Project R & D, what are the free brain mapping tools that are easy to use

PMP项目管理概述
随机推荐
PHP的exec函数
對補wasm環境的一些測試
Koa 快速入门
Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
PHP XML expat parser
Trigonometric function calculation
"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
【無標題】
Relationship between EMC, EMI and EMS
PMP商业分析概述
Table implements alternative adaptation through pseudo classes
矩阵特征值和特征向量求解——特征值分解(EVD)
Oracle Recovery Tools实战批量坏块修复
QT basics tutorial: qstringlist
微信小程序自定义组件
Chrome browser close update Popup
Use code binding DataGridView control to display tables in program interface
字符串分段组合
Why should the pointer be null after delete
Tuples of combined data types