当前位置:网站首页>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 03:28:00 【Andy Python】
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

边栏推荐
- 云成本优化有哪些优秀实践?
- Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
- __ getitem__ And__ setitem__
- 剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
- INFO:&nbsp;HHH000397:&nbsp;Using…
- 2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作
- 项目实战!手把手教你 Jmeter 性能测试
- What is the best and safest software to download when buying stocks?
- nn. Parameter and torch nn. Init series of functions to initialize model parameters
- Basic operation of stack (implemented in C language)
猜你喜欢

基于 WPF 的酷炫 GUI 窗口的简易实现

In the digital era, enterprises must do well in user information security

Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)

Object类,以及__new__,__init__,__setattr__,__dict__

Tips for visiting the website: you are not authorized to view the recovery method of this page

Flow based depth generation model

为什么OpenCV计算的帧率是错误的?

__getitem__和__setitem__

Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队

Build your own website (17)
随机推荐
Is Guotai Junan Securities reliable? Is it safe to open a securities account?
Notepad++--列编辑模式--用法/实例
华为设备WLAN基本业务配置命令
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
在excel文件上设置下拉选项
service实现类里面为何一直报红
国泰君安证券靠谱吗?开证券账户安全吗?
R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision
Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
无代码软件发展简史及未来趋势
Basic operation of stack (implemented in C language)
SSH框架的搭建(上)
Yes, it's about water
【iptables&icmp】iptables默认策略中关于icmp协议的说明
新手开哪家的证券账户是比较好?股票开户安全吗
composition api在项目中的使用总结
Apache - Introduction à Apache
数的每一位平方和
为什么OpenCV计算的帧率是错误的?
Dataloader参数collate_fn的使用