当前位置:网站首页>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
- Stability summary
- Workplace tips | understanding the advantages of the position "knowing people"
- [Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
- Please allow the "imperfection" of the current domestic Tob
- Flask——总结
- LeetCode:二叉树展开为链表_114
- Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
- Lumiprobe protein labeling research scheme
- Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
猜你喜欢

【激活函数】

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)

Usage example of qjsonobject

Study on bifunctional crosslinker lumiprobe sulfoacyanine 7 dicarboxylic acid

力扣树的进一步应用

Interface test process

with torch. no_ Grad(): reason for using

Apisik helps Middle East social software realize localized deployment

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik

Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
随机推荐
Leetcode56. consolidation interval
LeetCode每日一题——30. 串联所有单词的子串
精通数据分析能力,收入翻倍?什么才是最强竞争力
Leetcode daily question - 324 Swing sort II
【读书会第13期】视频文件的封装格式
Mongodb - replica set and sharding
APISIX 助力中东社交软件,实现本地化部署
在哪个软件上开户比较安全,开户流程是什么?
Query rewriting for opengauss kernel analysis
mysql-发生系统错误1067
with torch. no_ Grad(): reason for using
Pyechart drawing multiple Y-axis line graphs
LeetCode每日一题——710. 黑名单中的随机数
什么是接口?什么是接口测试?
【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
Globalsign's Pan domain SSL certificate
User network model and QoE
LeetCode56. 合并区间
Leetcode daily question - Sword finger offer II 091 Paint the house
Usage example of qjsonobject