当前位置:网站首页>18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点
18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点
2022-06-29 20:09: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. 总结

边栏推荐
- 剑指 Offer 41. 数据流中的中位数
- Foxit software was invited to appear at the 2022 advanced manufacturing digital intelligence development forum
- How to set a pod to run on a specified node
- SSH command and instructions
- ETCD数据库源码分析——服务端PUT流程
- Startservice() procedure
- Luoqingqi: has high-end household appliances become a red sea? Casati took the lead in breaking the game
- Nutch2.1 using eclipse debug to store the build process in MySQL on the windows platform
- 剑指 Offer 59 - II. 队列的最大值
- MySQL remote connection
猜你喜欢

一个超赞的开源的图片去水印解决方案

4-1 port scanning technology
![[observation] softcom power liutianwen: embrace change and](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy

Flume配置4——自定义Source+Sink

Deficiencies and optimization schemes in Dao

MySQL remote connection

苹果iPhone手机升级系统内存空间变小不够如何解决?

As the "only" privacy computing provider, insight technology is the "first" to settle in the Yangtze River Delta data element circulation service platform

Linux安装MySQL5

软件测试逻辑覆盖相关理解
随机推荐
Snowflake ID, distributed unique ID
Nutch2.1分布式抓取
Sword finger offer 59 - I. maximum value of sliding window
2022年深圳市福田区支持先进制造业发展若干措施
命令执行(RCE)漏洞
JMeter BeanShell explanation and thread calling
2021 CCPC 哈尔滨 E. Power and Modulo (思维题)
[notes] take notes again -- learn by doing Verilog HDL – 014
Flume ng configuration
How to use filters in jfinal to monitor Druid for SQL execution?
Spark存储体系底层架构剖析-Spark商业环境实战
【精品】pinia详解
XSS vulnerability
Ovirt database modify delete node
[compilation principle] syntax analysis
Oracle保留字查询
Withdrawal of user curve in qualified currency means loss
Nutch2.1 using eclipse debug to store the build process in MySQL on the windows platform
go: 如何编写一个正确的udp服务端
[compilation principle] semantic analysis