当前位置:网站首页>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 (思维题)
- Tag based augmented reality using OpenCV
- Flume配置4——自定義Source+Sink
- SSH命令及使用说明
- [observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy
- Summary of swift optional values
- 雪花id,分布式唯一id
- Tiger painter mengxiangshun's digital collection is on sale in limited quantities and comes with Maotai in the year of the tiger
- 童年经典蓝精灵之百变蓝爸爸数字藏品中奖名单公布
- 软件工程—原理、方法与应用
猜你喜欢

Jmeter之BeanShell详解和夸线程调用

lock4j--分布式锁中间件--自定义获取锁失败的逻辑
![[notes] take notes again -- learn by doing Verilog HDL – 014](/img/92/ba794253f1588ff9ad87d2571a453e.png)
[notes] take notes again -- learn by doing Verilog HDL – 014

Lock4j -- distributed lock Middleware -- customize the logic of lock acquisition failure

Flume configuration 1 - basic case
![[compilation principle] type check](/img/fc/458871e2df4e0384f65e09faa909d7.png)
[compilation principle] type check

Flutter calls Baidu map app to realize location search and route planning
![[notes] take notes again -- learn by doing Verilog HDL – 008](/img/7f/0ca73446247455ac4d8f9667083a87.png)
[notes] take notes again -- learn by doing Verilog HDL – 008

Bigder:自动化测试工程师

CorelDRAW2022全新版V24.1.0.360更新
随机推荐
Deficiencies and optimization schemes in Dao
A great open source image watermarking solution
[notes] take notes again -- learn by doing Verilog HDL – 014
Linux安装MySQL5
wangeditor富文本编辑器使用(详细)
3-2 host discovery - layer 3 discovery
Defense cornerstone in attack and defense drill -- all-round monitoring
Linux Installation mysql8
Startservice() procedure
[network orientation training] - Enterprise Park Network Design - [had done]
Flume theory
Performance improvement at the cost of other components is not good
Hangfire详解
Golang基础学习
fastadmin后台设置单选按钮
Zotero期刊自動匹配更新影響因子
Zotero期刊自动匹配更新影响因子
Tiger painter mengxiangshun's digital collection is on sale in limited quantities and comes with Maotai in the year of the tiger
Lock4j -- distributed lock Middleware -- customize the logic of lock acquisition failure
[compilation principle] syntax analysis