当前位置:网站首页>16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
2022-06-28 21:21:00 【安迪python学习笔记】
16 bs对象.节点名div.属性contents children descendants 获取子节点 子孙节点
文章目录
1. 关联选择
<p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p>
上述HTML代码有4个p节点,我们之前的节点选择方法只能选取到第一个p节点。
要选取第2、3、4p节点,我们就需要利用关联选择方法。

2. bs对象.节点名.属性contents 获取子节点
contents [kənˈtents]:内容。
语法格式:bs对象.节点名.contents
返回值:列表
取值方法:列表[索引]
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """ <html> <head><meta charset="utf-8"><title>古诗2首</title></head> <body> <div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div> <div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3> <p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p> </div> </body> </html> """
# 1. 从bs4解析库导入BeautifulSoup类用于解析数据
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入2个参数,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名div——获取第1个div节点:")
print(bs_duixiang.div,'\n')
print("步骤2.1:bs对象.节点名div.属性contents——获取第1个div节点的所有子节点:")
print(bs_duixiang.div.contents,'\n')
print("步骤2.2 :属性contents得到的子节点的数据类型为列表:")
print(type(bs_duixiang.div.contents),'\n')
print("步骤3:用列表[索引]——取值:",'\n')
print("div节点的第1个子节点:",bs_duixiang.div.contents[0],'\n')
print("div节点的第2个子节点:",bs_duixiang.div.contents[1],'\n')
print("div节点的第5个子节点:",bs_duixiang.div.contents[4],'\n')
print("div节点的第5个子节点的文本内容:",bs_duixiang.div.contents[4].string,'\n')
【终端输出】
步骤1:bs对象.节点名div——获取第1个div节点:
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
步骤2.1:bs对象.节点名div.属性contents——获取第1个div节点的所有子节点:
[<h2>如梦令</h2>, <h3>李清照<span>(宋)</span></h3>, <p>试问卷帘人</p>, <p>却道海棠依旧</p>, <p>知否?知否?</p>, <p>应是绿肥红瘦</p>]
步骤2.2 :属性contents得到的子节点的数据类型为列表:
<class 'list'>
步骤3:用列表[索引]——取值:
div节点的第1个子节点: <h2>如梦令</h2>
div节点的第2个子节点: <h3>李清照<span>(宋)</span></h3>
div节点的第5个子节点: <p>知否?知否?</p>
div节点的第5个子节点的文本内容: 知否?知否?
3. bs对象.节点名.children 获取子节点
children[ˈtʃɪldrən]:孩子们。
语法格式:bs对象.节点名.children
返回值:列表迭代器。
取值方法:用for循环从列表迭代器中取值。for i,child in enumerate(bs对象.节点名.children)
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """ <html> <head><meta charset="utf-8"><title>古诗2首</title></head> <body> <div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div> <div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3> <p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p> </div> </body> </html> """
# 1. 从bs4解析库导入BeautifulSoup类用于解析数据
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入2个参数,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名div——获取第1个div节点:")
print(bs_duixiang.div,'\n')
print("步骤2.1:bs对象.节点名div.属性children——获取第1个div节点的所有子节点:")
print(bs_duixiang.div.children,'\n')
print("步骤2.2 :属性children得到的子节点的数据类型为列表迭代器:")
print(type(bs_duixiang.div.children),'\n')
print("步骤3.1:用for循环从列表迭代器中取值:")
for i,child in enumerate(bs_duixiang.div.children) :
print(i, child)
# '\n'的作用是输出一个空行,目的是为让输出内容美观,方便查看
print('\n')
print("步骤3.2 从列表迭代器中取出的child的数据类型为tag对象:")
print(type(child),'\n')
print("步骤4:输出第1个div节点最后一个子节点的文本内容:")
print(child.string,'\n')
【终端输出】
步骤1:bs对象.节点名div——获取第1个div节点:
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
步骤2.1:bs对象.节点名div.属性children——获取第1个div节点的所有子节点:
<list_iterator object at 0x0000012FAE97B160>
步骤2.2 :属性children得到的子节点的数据类型为列表迭代器:
<class 'list_iterator'>
步骤3.1:用for循环从列表迭代器中取值:
0 <h2>如梦令</h2>
1 <h3>李清照<span>(宋)</span></h3>
2 <p>试问卷帘人</p>
3 <p>却道海棠依旧</p>
4 <p>知否?知否?</p>
5 <p>应是绿肥红瘦</p>
步骤3.2 从列表迭代器中取出的child的数据类型为tag对象:
<class 'bs4.element.Tag'>
步骤4:输出第1个div节点最后一个子节点的文本内容:
应是绿肥红瘦
for i,child in enumerate(bs_duixiang.div.children) 中enumerate的用法参考相关笔记。
4. bs对象.节点名.descendants 获取子孙节点
descendants [dɪˈsendənts]:后代。
generator [ˈdʒenəreɪtə]:生成器。
语法格式:bs对象.节点名.descendants
返回值:生成器。
取值方法:用for循环从生成器中取值。for i,child in enumerate(bs对象.节点名.descendants)
# 声明一个html_str字符串变量,存储部分HTML代码
html_str = """ <html> <head><meta charset="utf-8"><title>古诗2首</title></head> <body> <div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div> <div class="poems" id="section2"><h2>无题</h2><h3>佚名</h3> <p>本是青灯不归客</p><p>却因浊酒留风尘</p><p>终是庄周梦了蝶</p><p>你是恩赐也是劫</p> </div> </body> </html> """
# 1. 从bs4解析库导入BeautifulSoup类用于解析数据
from bs4 import BeautifulSoup
# 2.1 BeautifulSoup类(要解析的字符串,解析器)
# 2.2 传入2个参数,实例化类得到一个BeautifulSoup对象
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("步骤1:bs对象.节点名div——获取第1个div节点:")
print(bs_duixiang.div,'\n')
print("步骤2.1:bs对象.节点名div.属性descendants—获取第1个div节点的所有子孙节点:")
print(bs_duixiang.div.descendants,'\n')
print("步骤2.2 :属性descendants得到的子孙节点的数据类型为生成器:")
print(type(bs_duixiang.div.descendants),'\n')
print("步骤3:用for循环从生成器中取值:")
for i, desc in enumerate(bs_duixiang.div.descendants) :
print(i, desc )
【终端输出】
步骤1:bs对象.节点名div——获取第1个div节点:
<div class="poems" id="section1"><h2>如梦令</h2><h3>李清照<span>(宋)</span></h3><p>试问卷帘人</p><p>却道海棠依旧</p><p>知否?知否?</p><p>应是绿肥红瘦</p></div>
步骤2.1:bs对象.节点名div.属性descendants—获取第1个div节点的所有子孙节点:
<generator object Tag.descendants at 0x0000012FAE8F0200>
步骤2.2 :属性descendants得到的子孙节点的数据类型为生成器:
<class 'generator'>
步骤3:用for循环从生成器中取值:
0 <h2>如梦令</h2>
1 如梦令
2 <h3>李清照<span>(宋)</span></h3>
3 李清照
4 <span>(宋)</span>
5 (宋)
6 <p>试问卷帘人</p>
7 试问卷帘人
8 <p>却道海棠依旧</p>
9 却道海棠依旧
10 <p>知否?知否?</p>
11 知否?知否?
12 <p>应是绿肥红瘦</p>
13 应是绿肥红瘦
边栏推荐
- rapid ssl通配符证书八百一年是正版吗
- Postman introduction and installation steps
- Explanation of memory dump triggered by software watchdog and anr
- LeetCode123. The best time to buy and sell stocks III
- List of domestic database directory
- Activate function
- 题解 The SetStack Computer(UVa12096)紫书P116STL的综合应用
- Microsoft's exclusive payment function has also been perfectly unlocked
- Bitbucket 使用 SSH 拉取仓库失败的问题
- 职场小技巧 | 了解岗位优势三板斧之“识人”
猜你喜欢

Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox

Query rewriting for opengauss kernel analysis

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

What is an interface? What is interface testing?

Postman introduction and installation steps

The further application of Li Kou tree

Ehcache configuration data, convenient for self checking

MySQL system error occurred 1067

Leetcode 36. Effective Sudoku (yes, once)

Interface test process
随机推荐
Is the rapid SSL wildcard certificate genuine in 1981
LeetCode226. Flip binary tree
Flask - Summary
稳定性总结
软件watchdog和ANR触发memory dump讲解
基于 Apache APISIX 的自动化运维平台
User network model and QoE
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
Pyechart drawing multiple Y-axis line graphs
Explanation of memory dump triggered by software watchdog and anr
Interface use case design
Leetcode daily question - Sword finger offer II 091 Paint the house
LeetCode每日一题——30. 串联所有单词的子串
【读书会第13期】视频文件的封装格式
QStringLiteral(str)
Leetcode daily question - 515 Find the maximum value in each tree row
Activate function
ANR分析--问题1
Deep interpretation of WiFi security vulnerability krack
pyechart绘制多条y轴折线图