当前位置:网站首页>18. `bs object Node name next_ sibling` previous_ Sibling get sibling node
18. `bs object Node name next_ sibling` previous_ Sibling get sibling node
2022-06-29 20:12:00 【Andy Python learning notes】
18. bs object . The node name .next_sibling previous_sibling Get brother nodes
List of articles
- 18. `bs object . The node name .next_sibling` previous_sibling Get brother nodes
- 1. `bs object . The node name .next_sibling` Get the next sibling node
- 2. `bs object . The node name .next_siblings` Get all the following sibling nodes
- 3. `bs object . The node name .previous_sibling` Gets the previous sibling node
- 4. `bs object . The node name .previous_siblings` Get all the previous sibling nodes
- 5. summary
1. bs object . The node name .next_sibling Get the next sibling node

sibling [ˈsɪblɪŋ]: brother .
Grammar format :bs object . The node name .next_sibling
Return value : Node object .
# Make a statement html_str String variable , Storage part HTML Code
html_str = """ <html> <head><meta charset="utf-8"><title> Ancient poetry 2 The first </title></head> <body> <div class="poems" id="section1"><h2> Like a dream </h2><h3> Li qingzhao <span>( The song dynasty )</span></h3><p> Let's ask the roller shutter </p><p> But the Begonia is still </p><p> To know whether ? To know whether ?</p><p> It should be green, fat, red and thin </p></div> <div class="poems" id="section2"><h2> Untitled </h2><h3> anonymous </h3> <p> This is blue lantern </p><p> But because of the wine left dust </p><p> Finally, Zhuang Zhou dreamed of butterfly </p><p> You are a gift and a curse </p> </div> </body> </html> """
# 1. from bs4 Parsing library import BeautifulSoup Class is used to parse data
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup class ( The string to parse , Parser )
# 2.2 Pass in the string to parse html_str And parsers lxml, Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name p—— For the first 1 individual p node :")
print(bs_duixiang.p,'\n')
print(" step 2.1:bs object . The node name p.next_sibling—— For the first 2 individual p node :")
print(bs_duixiang.p.next_sibling,'\n')
print(" step 2.2: The obtained data type is tag object :")
print(type(bs_duixiang.p.next_sibling),'\n')
【 Terminal output 】
step 1:bs object . The node name p—— For the first 1 individual p node :
<p> Let's ask the roller shutter </p>
step 2.1:bs object . The node name p.next_sibling—— For the first 2 individual p node :
<p> But the Begonia is still </p>
step 2.2: The obtained data type is tag object :
<class 'bs4.element.Tag'>
2. bs object . The node name .next_siblings Get all the following sibling nodes
Grammar format :bs object . The node name .next_siblings
Return value : generator .
Method of value taking :
Method 1:for Loop takes value from generator for i,child in enumerate(bs_duixiang.p.next_siblings)
Method 2: use list Function takes value from generator list(enumerate(bs_duixiang.p.next_siblings))
# Make a statement html_str String variable , Storage part HTML Code
html_str = """ <html> <head><meta charset="utf-8"><title> Ancient poetry 2 The first </title></head> <body> <div class="poems" id="section1"><h2> Like a dream </h2><h3> Li qingzhao <span>( The song dynasty )</span></h3><p> Let's ask the roller shutter </p><p> But the Begonia is still </p><p> To know whether ? To know whether ?</p><p> It should be green, fat, red and thin </p></div> <div class="poems" id="section2"><h2> Untitled </h2><h3> anonymous </h3> <p> This is blue lantern </p><p> But because of the wine left dust </p><p> Finally, Zhuang Zhou dreamed of butterfly </p><p> You are a gift and a curse </p> </div> </body> </html> """
# 1. from bs4 Parsing library import BeautifulSoup Class is used to parse data
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup class ( The string to parse , Parser )
# 2.2 Pass in the string to parse html_str And parsers lxml, Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name p—— For the first 1 individual p node :")
print(bs_duixiang.p,'\n')
print(" step 2.1:bs object . The node name p.next_siblings—— For the first 1 individual p All sibling nodes behind the node :")
print(bs_duixiang.p.next_siblings,'\n')
print(" step 2.2: The obtained data type is generator :")
print(type(bs_duixiang.p.next_siblings),'\n')
print(" Method of value taking 1: utilize for Loop takes value from generator ,i Represents the element sequence number ,child Show elements :")
for i, child in enumerate(bs_duixiang.p.next_siblings) :
print(i, child)
print('\n')
print(" Method of value taking 2: use list Function takes value from generator , What you get is a list :")
print(list(enumerate(bs_duixiang.p.next_siblings)))
【 Terminal output 】
step 1:bs object . The node name p—— For the first 1 individual p node :
<p> Let's ask the roller shutter </p>
step 2.1:bs object . The node name p.next_siblings—— For the first 1 individual p All sibling nodes behind the node :
<generator object PageElement.next_siblings at 0x000001C8C4BD46D0>
step 2.2: The obtained data type is generator :
<class 'generator'>
Method of value taking 1: utilize for Loop takes value from generator ,i Represents the element sequence number ,child Show elements :
0 <p> But the Begonia is still </p>
1 <p> To know whether ? To know whether ?</p>
2 <p> It should be green, fat, red and thin </p>
Method of value taking 2: use list Function takes value from generator , What you get is a list :
[(0, <p> But the Begonia is still </p>), (1, <p> To know whether ? To know whether ?</p>), (2, <p> It should be green, fat, red and thin </p>)]
3. bs object . The node name .previous_sibling Gets the previous sibling node
previous [ˈpriːviəs]: Ahead .
Grammar format :bs object . The node name .previous_sibling
Return value : Node object .
# Make a statement html_str String variable , Storage part HTML Code
html_str = """ <html> <head><meta charset="utf-8"><title> Ancient poetry 2 The first </title></head> <body> <div class="poems" id="section1"><h2> Like a dream </h2><h3> Li qingzhao <span>( The song dynasty )</span></h3><p> Let's ask the roller shutter </p><p> But the Begonia is still </p><p> To know whether ? To know whether ?</p><p> It should be green, fat, red and thin </p></div> <div class="poems" id="section2"><h2> Untitled </h2><h3> anonymous </h3> <p> This is blue lantern </p><p> But because of the wine left dust </p><p> Finally, Zhuang Zhou dreamed of butterfly </p><p> You are a gift and a curse </p> </div> </body> </html> """
# 1. from bs4 Parsing library import BeautifulSoup Class is used to parse data
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup class ( The string to parse , Parser )
# 2.2 Pass in the string to parse html_str And parsers lxml, Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name p—— For the first 1 individual p node :")
print(bs_duixiang.p,'\n')
print(" step 2.1:bs object . The node name .previous_siblingg—— For the first p In front of the node h3 node :")
print(bs_duixiang.p.previous_sibling,'\n')
print(" step 2.2: The obtained data type is tag object :")
print(type(bs_duixiang.p.previous_sibling),'\n')
【 Terminal output 】
step 1:bs object . The node name p—— For the first 1 individual p node :
<p> Let's ask the roller shutter </p>
step 2.1:bs object . The node name .previous_siblingg—— For the first p In front of the node h3 node :
<h3> Li qingzhao <span>( The song dynasty )</span></h3>
step 2.2: The obtained data type is tag object :
<class 'bs4.element.Tag'>
4. bs object . The node name .previous_siblings Get all the previous sibling nodes
Grammar format :bs object . The node name .previous_siblings
Return value : generator .
Method of value taking :
Method 1:for Loop takes value from generator for i,child in enumerate(bs_duixiang.p.previous_siblings)
Method 2: use list Function takes value from generator list(enumerate(bs_duixiang.p.previous_siblings))
# Make a statement html_str String variable , Storage part HTML Code
html_str = """ <html> <head><meta charset="utf-8"><title> Ancient poetry 2 The first </title></head> <body> <div class="poems" id="section1"><h2> Like a dream </h2><h3> Li qingzhao <span>( The song dynasty )</span></h3><p> Let's ask the roller shutter </p><p> But the Begonia is still </p><p> To know whether ? To know whether ?</p><p> It should be green, fat, red and thin </p></div> <div class="poems" id="section2"><h2> Untitled </h2><h3> anonymous </h3> <p> This is blue lantern </p><p> But because of the wine left dust </p><p> Finally, Zhuang Zhou dreamed of butterfly </p><p> You are a gift and a curse </p> </div> </body> </html> """
# 2.1 BeautifulSoup class ( The string to parse , Parser )
# 2.2 Pass in the string to parse html_str And parsers lxml, Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name p—— For the first 1 individual p node :")
print(bs_duixiang.p,'\n')
print(" step 2.1:bs object . The node name p.previous_siblings—— For the first 1 individual p All sibling nodes in front of the node :")
print(bs_duixiang.p.previous_siblings,'\n')
print(" step 2.2: The obtained data type is generator :")
print(type(bs_duixiang.p.previous_siblings),'\n')
print(" Method of value taking 1: utilize for Loop takes value from generator ,i Represents the element sequence number ,child Show elements :")
for i,child in enumerate(bs_duixiang.p.previous_siblings) :
print(i, child)
print('\n')
print(" Method of value taking 2: use list Function takes value from generator , What you get is a list :")
print(list(enumerate(bs_duixiang.p.previous_siblings)))
【 Terminal output 】
step 1:bs object . The node name p—— For the first 1 individual p node :
<p> Let's ask the roller shutter </p>
step 2.1:bs object . The node name p.previous_siblings—— For the first 1 individual p All sibling nodes in front of the node :
<generator object PageElement.previous_siblings at 0x000001C8C4B05900>
step 2.2: The obtained data type is generator :
<class 'generator'>
Method of value taking 1: utilize for Loop takes value from generator ,i Represents the element sequence number ,child Show elements :
0 <h3> Li qingzhao <span>( The song dynasty )</span></h3>
1 <h2> Like a dream </h2>
Method of value taking 2: use list Function takes value from generator , What you get is a list :
[(0, <h3> Li qingzhao <span>( The song dynasty )</span></h3>), (1, <h2> Like a dream </h2>)]
5. summary

边栏推荐
- 2021 CCPC 哈尔滨 J. Local Minimum (思维题)
- 畫虎國手孟祥順數字藏品限量發售,隨贈虎年茅臺
- How to use the configuration in thinkphp5
- Zotero journal Automatic Matching Update Influencing Factors
- [USB flash disk test] in order to transfer the data at the bottom of the pressure box, I bought a 2T USB flash disk, and the test result is only 47g~
- 0/1分数规划专题
- MySQL remote connection
- Summary of swift optional values
- XSS vulnerability
- 一个超赞的开源的图片去水印解决方案
猜你喜欢

Flume configuration 1 - basic case

Linux Installation mysql5
![[compilation principle] semantic analysis](/img/2e/9c17da3dbc758b2985e55201c73f99.png)
[compilation principle] semantic analysis

Withdrawal of user curve in qualified currency means loss
![[USB flash disk test] in order to transfer the data at the bottom of the pressure box, I bought a 2T USB flash disk, and the test result is only 47g~](/img/c3/e0637385d35943f1914477bb9f2b54.png)
[USB flash disk test] in order to transfer the data at the bottom of the pressure box, I bought a 2T USB flash disk, and the test result is only 47g~

mapbox-gl开发教程(十二):加载面图层数据

Linux安装MySQL8

Hangfire details

Three.js开发:粗线的画法

How to solve the problem of insufficient memory space in Apple iPhone upgrade system?
随机推荐
Flume configuration 1 - basic case
如何设置 Pod 到指定节点运行
Dynamics crm: among locally deployed servers, sandbox, unzip, VSS, asynchronous and monitor services
Flume配置1——基础案例
SSH命令及使用说明
Flume configuration 3 - interceptor filtering
剑指 Offer 59 - I. 滑动窗口的最大值
Sword finger offer 41 Median in data stream
[compilation principle] semantic analysis
go: 如何编写一个正确的udp服务端
一次 Keepalived 高可用的事故,让我重学了一遍它!
Zotero journal automatic matching update influence factor
4-1 port scanning technology
Dynamics CRM: 本地部署的服务器中, Sandbox, Unzip, VSS, Asynchronous还有Monitor服务的作用
PowerShell command outputs only a list of directories
0/1分数规划专题
[boutique] detailed explanation of Pinia
Linux Installation mysql8
攻防演练中的防守基石——全方位监控
Bigder:自动化测试工程师