当前位置:网站首页>18. `bs object Node name next_ Sibling` get sibling nodes
18. `bs object Node name next_ Sibling` get sibling nodes
2022-06-29 02:37:00 【Andy Python】
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

边栏推荐
- Tuples of combined data types
- 大智慧手机股票开户哪个券商更安全更方便?
- PHP database ODBC
- "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
- 安装kibana
- Three methods of time series prediction: statistical model, machine learning and recurrent neural network
- Install kibana
- There is a time delay for the click event on the mobile terminal. What is the delay time? How to solve it?
- leetcode 统计无向图中无法互相到达点对数
- PHP XML expat parser
猜你喜欢

KOA Quick Start

Understanding and design of high concurrency

高并发的理解与设计方案

CTFHub-Web-密码口令-弱口令

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

安装kibana

Ctfhub web password weak password

Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter

Deploy redis high availability cluster

Pvcreate ASM disk causes abnormal recovery of ASM disk group - sparing separation
随机推荐
String output
Koa 快速入門
Wechat applet custom component
Pyinstaller打包pikepdf失败的问题排查
SystemVerilog structure (I)
To apply for a test engineer after years, the resume with high scores should be written like this
Tuples of combined data types
Day10 enumeration class and annotation
瀑布型项目管理最常用的10个小工具,可以自由搭建使用
Relations EMC, EMI, EMS
Use photoshop2022 to create a wonderful gradient effect for pictures
Com3529 test analysis
组合数据类型之元组小练习
對補wasm環境的一些測試
Has Moore's law come to an end?
Is it safe to contact the account manager online to open an account for stock speculation?
Project R & D, what are the free brain mapping tools that are easy to use
Informatics Olympiad all in one 1361: production | Luogu P1037 [noip2002 popularization group] production
高并发的理解与设计方案
mgalcu-a509