当前位置:网站首页>17 `bs object Node name h3 Parent ` parents get parent node ancestor node
17 `bs object Node name h3 Parent ` parents get parent node ancestor node
2022-06-28 21:22:00 【Andy Python learning notes】
17 bs object . The node name h3.parent parents Get parent node Ancestral node
1. bs object . The node name h3.parent Get parent node
parent [ˈpeərənt]: father .
Grammar format :bs object . The node name h3.parent
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 h3—— Take it to 1 individual h3 node :")
print(bs_duixiang.h3,'\n')
print(" step 2.1:bs object . The node name h3.parent—— Take it to h3 Parent of node div node :")
print(bs_duixiang.h3.parent,'\n')
print(" step 2.2 View the data type of the node :")
print(type(bs_duixiang.h3.parent),'\n')
【 Terminal output 】
step 1:bs object . The node name h3—— Take it to 1 individual h3 node :
<h3> Li qingzhao <span>( The song dynasty )</span></h3>
step 2.1:bs object . The node name h3.parent—— Take it to h3 Parent of node div node :
<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>
step 2.2 View the data type of the node :
<class 'bs4.element.Tag'>
2. bs object . The node name h3.parents Get ancestor nodes
enumerate [ɪˈnjuːməreɪt]: list , stay Python Add enumerate The element index can be output .
Grammar format :bs object . The node name .parents
Return value : generator
Method of value taking :
Method 1:for Loop takes value from generator for i,child in enumerate(bs_duixiang.h3.parents)
Method 2: use list Function takes value from generator list(enumerate(bs_duixiang.h3.parents))
2.1 for Loop takes value from generator
# 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 h3—— Take it to 1 individual h3 node :")
print(bs_duixiang.h3,'\n')
print(" step 2.1:bs object . The node name h3.parents—— Take it to h3 The ancestor node of the node :")
print(bs_duixiang.h3.parents,'\n')
print(" step 2.2 View the data type of the node :")
print(type(bs_duixiang.h3.parents),'\n')
print(" step 3:for Loop takes value from generator :")
for i,child in enumerate(bs_duixiang.h3.parents) :
print(i, child)
【 Terminal output 】
step 1:bs object . The node name h3—— Take it to 1 individual h3 node :
<h3> Li qingzhao <span>( The song dynasty )</span></h3>
step 2.1:bs object . The node name h3.parents—— Take it to h3 The ancestor node of the node :
<generator object PageElement.parents at 0x0000017B0D5144A0>
step 2.2 View the data type of the node :
<class 'generator'>
step 3:for Loop takes value from generator :
0 <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>
1 <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>
2 <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>
3 <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.2 use list Function takes value from generator
# 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 h3—— Take it to 1 individual h3 node :")
print(bs_duixiang.h3,'\n')
print(" step 2.1:bs object . The node name h3.parents—— Take it to h3 The ancestor node of the node :")
print(bs_duixiang.h3.parents,'\n')
print(" step 2.2 View the data type of the node :")
print(type(bs_duixiang.h3.parents),'\n')
print(" step 3: use list Function takes value from generator :")
print(list(enumerate(bs_duixiang.h3.parents)))
【 Terminal output 】
step 1:bs object . The node name h3—— Take it to 1 individual h3 node :
<h3> Li qingzhao <span>( The song dynasty )</span></h3>
step 2.1:bs object . The node name h3.parents—— Take it to h3 The ancestor node of the node :
<generator object PageElement.parents at 0x0000017B0D514580>
step 2.2 View the data type of the node :
<class 'generator'>
step 3: use list Function takes value from generator :
[(0, <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>), (1, <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>), (2, <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>), (3, <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>
)]
After running the code , Get a list .
The top number of the list 0,1,2,3 Is the index of the element .
The first 1 The element is h3 Parent of node div node .
The first 2 The element is div The parent of the node body node .
The first 3 The element is body Parent of node html node .
The first 4 Elements are all nodes .
for Circulation and list The final output of the function is the same , It's just list The output of the function is in the form of a list .
3. summary

4. attach html Code
<html> <!--html Node is body Parent of node , yes h3 The ancestor node of the node -->
<head> <!--head Nodes and div Node level , No h3 The ancestor node of the node -->
<meta charset="utf-8">
<title> Ancient poetry 2 The first </title>
</head>
<body> <!--body Node is div Parent of node , yes h3 The ancestor node of the node -->
<div class="poems" id="section1"> <!--div Node is h3 Parent of node -->
<h2> Like a dream </h2>
<h3> Li qingzhao <span>( The song dynasty )</span></h3> <!-- With h3 Node as the benchmark -->
<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>
Sample web page

边栏推荐
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
- QJsonObject的使用示例
- Leetcode daily question - Sword finger offer II 091 Paint the house
- LeetCode877. Stone game
- LeetCode每日一题——324. 摆动排序 II
- rapid ssl通配符证书八百一年是正版吗
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- LeetCode56. 合并区间
- Leetcode daily question - 30 Concatenate substrings of all words
- 如何使用 DataAnt 监控 Apache APISIX
猜你喜欢

力扣树的进一步应用

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

Ehcache配置资料,方便自己查

User network model and QoE
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

如何使用 DataAnt 监控 Apache APISIX

QJsonObject的使用示例

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

APISIX 助力中东社交软件,实现本地化部署

MongoDB——副本集与分片
随机推荐
Recommend two high-quality Wallpaper software
LeetCode116. Populate the next right node pointer for each node
Understand the construction of the entire network model
LeetCode:二叉树展开为链表_114
Explanation and usage of sqrt() function
Application of the purple book p113map of ananagrams (uva156)
如何使用 DataAnt 监控 Apache APISIX
LeetCode每日一题——剑指 Offer II 091. 粉刷房子
GlobalSign的泛域名SSL证书
题解 The Blocks Problem(UVa101)紫书P110vector的应用
Activate function
MySQL system error occurred 1067
LeetCode116. 填充每个节点的下一个右侧节点指针
What is an interface? What is interface testing?
Automatic operation and maintenance platform based on Apache APIs
Bitbucket failed to pull the warehouse Using SSH
LeetCode117. Populate the next right node pointer for each node_ II
认识Web自动化测试
Manual backup and restore of DHCP server
How to understand the fast iteration of cloud native database?