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

边栏推荐
- Why should the pointer be null after delete
- Today's sleep quality record 82 points
- Informatics Olympiad 1361: Produce
- EMC、EMI、EMS的關系
- Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first
- 2022.02.15
- [sans titre]
- 月薪没到30K的程序员必须要背的面试八股,我先啃为敬
- 110. simple chat room 13: chat room server
- Relations EMC, EMI, EMS
猜你喜欢

Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first

Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)

项目研发,有哪些好用的免费脑图工具软件

How to use project Gantt chart to make project report

mgalcu-a509

如何用项目甘特图,做好项目汇报

Target detection - ADAS practice

KOA Quick Start

Regular expression (?: pattern)

Leetcode counts the number of ways to place houses
随机推荐
Apache does not parse PHP files, but directly displays the source code
Project R & D, what are the free brain mapping tools that are easy to use
Troubleshooting of pyinstaller failed to pack pikepdf
Install kibana
They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?
apache不解析PHP文件,直接显示源码
mgalcu-a509
mark
学习太极创客 — MQTT 第二章(九)本章测试
Calculate rectangular area
LinkedList学习
Pytoch Learning Series: Introduction
OpenResty 使用介绍
Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first
Use photoshop2022 to create a wonderful gradient effect for pictures
Koa 快速入門
Com3529 test analysis
What is the Valentine's Day gift given by the operator to the product?
50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
Only in the past four years, Microsoft finally gave it up!