当前位置:网站首页>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

边栏推荐
- Application of Andy s first dictionary (uva10815) Purple Book p112set
- Understanding of incomplete types
- Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
- LeetCode116. 填充每个节点的下一个右侧节点指针
- [try to hack] cobalt strike (I)
- 稳定性总结
- [book club issue 13] packaging format of video files
- 1. integrate servlets
- Leetcode daily question - 324 Swing sort II
- LeetCode877. 石子游戏
猜你喜欢

Pie (poj3122) super detailed and easy to understand binary introduction

Leetcode daily question - 515 Find the maximum value in each tree row

Study on bifunctional crosslinker lumiprobe sulfoacyanine 7 dicarboxylic acid

Ehcache configuration data, convenient for self checking

【激活函数】

Mongodb - replica set and sharding

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer

什么是接口?什么是接口测试?

视频号如何下载视频?来看超简单方法!

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
随机推荐
Which software is safer to open an account on and what is the account opening process?
LeetCode122. The best time to buy and sell stocks II
开通挖财账号安全吗?是靠谱的吗?
with torch. no_ Grad(): reason for using
Proficient in data analysis, double the income? What is the strongest competitiveness
LeetCode121. 买卖股票的最佳时机
Is the rapid SSL wildcard certificate genuine in 1981
Building web automation environment
LeetCode123. 买卖股票的最佳时机III
How to understand the fast iteration of cloud native database?
Query rewriting for opengauss kernel analysis
Microsoft's exclusive payment function has also been perfectly unlocked
Please allow the "imperfection" of the current domestic Tob
Activate function
Leetcode: merge two ordered linked lists_ twenty-one
Bitbucket 使用 SSH 拉取仓库失败的问题
题解 The Blocks Problem(UVa101)紫书P110vector的应用
Definition and precautions of genuine St link/v2 j-link jtag/swd pin
Understanding web automated testing
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application