当前位置:网站首页> 18. `bs對象.節點名.next_sibling` 獲取兄弟節點
18. `bs對象.節點名.next_sibling` 獲取兄弟節點
2022-06-29 02:37: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. 總結

边栏推荐
- 字符串分段组合
- Pvcreate ASM disk causes abnormal recovery of ASM disk group - sparing separation
- Day10 enumeration class and annotation
- Studies of relative costs for development in different languages
- 短视频平台常见SQL面试题,你学会了吗?
- Pyinstaller打包pikepdf失败的问题排查
- B1009 irony
- 东方财富股票开户是会有什么风险吗?东方财富开户安全吗
- Leetcode counts the number of ways to place houses
- chrome浏览器关闭更新弹窗
猜你喜欢

Trigonometric function calculation

What is the Valentine's Day gift given by the operator to the product?

矩阵特征值和特征向量求解——特征值分解(EVD)

The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written

EMC、EMI、EMS的關系

sql连续登录问题

Install kibana

PMP项目管理概述

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

Redis master-slave replication
随机推荐
"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
Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first
哪个证券公司最大最安全 哪家券商服务好
月薪没到30K的程序员必须要背的面试八股,我先啃为敬
How does sound amplify weak sounds
安装mysql5.7 并修改密码
110. simple chat room 13: chat room server
安装kibana
chrome浏览器关闭更新弹窗
String length
String output
Pyinstaller打包pikepdf失败的问题排查
Handling method of occasional error reporting on overseas equipment
字符串替换
The meaning of cross multiplication and dot multiplication (simple formula memory method)
字符串分段组合
正则表达式(?:pattern)
【网络通信学习笔记】Socket.IO的搭建和部署
三角函数计算
MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year