当前位置:网站首页>16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants
16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants
2022-06-28 21:22:00 【Andy Python learning notes】
16 bs object . The node name div. attribute contents children descendants Get child nodes Descendants node
List of articles
1. Association selection
<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>
Above HTML The code has 4 individual p node , Our previous node selection method can only select the first one p node .
To choose the first 2、3、4p node , We need to use the association selection method .

2. bs object . The node name . attribute contents Get child nodes
contents [kənˈtents]: Content .
Grammar format :bs object . The node name .contents
Return value : list
Method of value taking : list [ Indexes ]
# 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 2 Parameters , Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name div—— For the first 1 individual div node :")
print(bs_duixiang.div,'\n')
print(" step 2.1:bs object . The node name div. attribute contents—— For the first 1 individual div All children of a node :")
print(bs_duixiang.div.contents,'\n')
print(" step 2.2 : attribute contents The data type of the obtained child nodes is list :")
print(type(bs_duixiang.div.contents),'\n')
print(" step 3: Use list [ Indexes ]—— Value :",'\n')
print("div Node No 1 Child node :",bs_duixiang.div.contents[0],'\n')
print("div Node No 2 Child node :",bs_duixiang.div.contents[1],'\n')
print("div Node No 5 Child node :",bs_duixiang.div.contents[4],'\n')
print("div Node No 5 Text content of child nodes :",bs_duixiang.div.contents[4].string,'\n')
【 Terminal output 】
step 1:bs object . The node name div—— For the first 1 individual 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.1:bs object . The node name div. attribute contents—— For the first 1 individual div All children of a node :
[<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>]
step 2.2 : attribute contents The data type of the obtained child nodes is list :
<class 'list'>
step 3: Use list [ Indexes ]—— Value :
div Node No 1 Child node : <h2> Like a dream </h2>
div Node No 2 Child node : <h3> Li qingzhao <span>( The song dynasty )</span></h3>
div Node No 5 Child node : <p> To know whether ? To know whether ?</p>
div Node No 5 Text content of child nodes : To know whether ? To know whether ?
3. bs object . The node name .children Get child nodes
children[ˈtʃɪldrən]: The children .
Grammar format :bs object . The node name .children
Return value : List iterator .
Method of value taking : use for Loop takes values from the list iterator .for i,child in enumerate(bs object . The node name .children)
# 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 2 Parameters , Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name div—— For the first 1 individual div node :")
print(bs_duixiang.div,'\n')
print(" step 2.1:bs object . The node name div. attribute children—— For the first 1 individual div All children of a node :")
print(bs_duixiang.div.children,'\n')
print(" step 2.2 : attribute children The data type of the resulting child node is list iterator :")
print(type(bs_duixiang.div.children),'\n')
print(" step 3.1: use for Loop takes values from the list iterator :")
for i,child in enumerate(bs_duixiang.div.children) :
print(i, child)
# '\n' The function of is to output a blank line , The purpose is to make the output content beautiful , Convenient view
print('\n')
print(" step 3.2 From the list iterator child The data type of is tag object :")
print(type(child),'\n')
print(" step 4: Output No 1 individual div The text content of the last child node of the node :")
print(child.string,'\n')
【 Terminal output 】
step 1:bs object . The node name div—— For the first 1 individual 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.1:bs object . The node name div. attribute children—— For the first 1 individual div All children of a node :
<list_iterator object at 0x0000012FAE97B160>
step 2.2 : attribute children The data type of the resulting child node is list iterator :
<class 'list_iterator'>
step 3.1: use for Loop takes values from the list iterator :
0 <h2> Like a dream </h2>
1 <h3> Li qingzhao <span>( The song dynasty )</span></h3>
2 <p> Let's ask the roller shutter </p>
3 <p> But the Begonia is still </p>
4 <p> To know whether ? To know whether ?</p>
5 <p> It should be green, fat, red and thin </p>
step 3.2 From the list iterator child The data type of is tag object :
<class 'bs4.element.Tag'>
step 4: Output No 1 individual div The text content of the last child node of the node :
It should be green, fat, red and thin
for i,child in enumerate(bs_duixiang.div.children) in enumerate Refer to relevant notes for usage of .
4. bs object . The node name .descendants Get children's node
descendants [dɪˈsendənts]: Progeny .
generator [ˈdʒenəreɪtə]: generator .
Grammar format :bs object . The node name .descendants
Return value : generator .
Method of value taking : use for Loop takes value from generator .for i,child in enumerate(bs object . The node name .descendants)
# 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 2 Parameters , Instantiate the class to get a BeautifulSoup object
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" step 1:bs object . The node name div—— For the first 1 individual div node :")
print(bs_duixiang.div,'\n')
print(" step 2.1:bs object . The node name div. attribute descendants— For the first 1 individual div All descendants of node :")
print(bs_duixiang.div.descendants,'\n')
print(" step 2.2 : attribute descendants The data type of the resulting descendant node is generator :")
print(type(bs_duixiang.div.descendants),'\n')
print(" step 3: use for Loop takes value from generator :")
for i, desc in enumerate(bs_duixiang.div.descendants) :
print(i, desc )
【 Terminal output 】
step 1:bs object . The node name div—— For the first 1 individual 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.1:bs object . The node name div. attribute descendants— For the first 1 individual div All descendants of node :
<generator object Tag.descendants at 0x0000012FAE8F0200>
step 2.2 : attribute descendants The data type of the resulting descendant node is generator :
<class 'generator'>
step 3: use for Loop takes value from generator :
0 <h2> Like a dream </h2>
1 Like a dream
2 <h3> Li qingzhao <span>( The song dynasty )</span></h3>
3 Li qingzhao
4 <span>( The song dynasty )</span>
5 ( The song dynasty )
6 <p> Let's ask the roller shutter </p>
7 Let's ask the roller shutter
8 <p> But the Begonia is still </p>
9 But the Begonia is still
10 <p> To know whether ? To know whether ?</p>
11 To know whether ? To know whether ?
12 <p> It should be green, fat, red and thin </p>
13 It should be green, fat, red and thin
边栏推荐
- How to use dataant to monitor Apache apisex
- Bitbucket failed to pull the warehouse Using SSH
- API gateway Apache APIs IX helps the evolution of snowball dual active architecture
- Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
- RT thread thread synchronization and thread communication
- Alist+raidrive gives the computer a complete 8billion GB hard disk drive
- Study on luminiprobe non fluorescent azide -- 3-azido propanol
- How to recover after Oracle delete accidentally deletes table data
- LeetCode1114. 按序打印
- LeetCode每日一题——515. 在每个树行中找最大值
猜你喜欢

Mongodb - replica set and sharding

题解 Pie(POJ3122)超详细易懂的二分入门

Alist+raidrive gives the computer a complete 8billion GB hard disk drive

The rogue downloader named by 315 is back

Usage example of qjsonobject

力扣树的进一步应用

Recommend two high-quality Wallpaper software

How to analyze the relationship between enterprise digital transformation and data asset management?

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application

Ehcache configuration data, convenient for self checking
随机推荐
Web自动化工具选择
ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
Mongodb - replica set and sharding
RT thread thread synchronization and thread communication
Ehcache configuration data, convenient for self checking
力扣树的进一步应用
How to use dataant to monitor Apache apisex
Leetcode: expand a binary tree into a linked list_ one hundred and fourteen
LeetCode每日一题——515. 在每个树行中找最大值
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
Leetcode daily question - Sword finger offer II 091 Paint the house
Understand the construction of the entire network model
Leetcode: merge K ascending linked lists_ twenty-three
【读书会第13期】视频文件的封装格式
The blocks problem (uva101) Purple Book p110vector application
PHP uses stack to solve maze problem
【激活函数】
Understanding web automated testing
Web 自动化环境搭建
Proficient in data analysis, double the income? What is the strongest competitiveness