子标签和后代标签: .children 和 .descendants

昨天看书,没有用enumurate枚举的时候,完全发觉不了他们的区别,倍感困惑。
今天看了其他人写的教程,用了枚举法,然后就发现他们之间的区别了。
还要注意,子代接下一个子代时,可能是换行符,看children找出的子代,你就可以体会到啦!
descendants是找了子标签,然后再一步步探入别人的家中,把一个个后台,按辈分由大往小找着。

html ="""
<html>
    <head>
        <title>The Dormouse's story</title>
    </head>
    <body>
        <p class="story">
            Once upon a time there were three little sisters; and their names were
            <a href="http://example.com/elsie" class="sister" id="link1">
                <span>Elsie</span>
            </a>
            <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>
            and
            <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
            and they lived at the bottom of a well.
        </p>
        <p class="story">...</p>
"""
from bs4 import BeautifulSoup

soup1 = BeautifulSoup(html, 'lxml')
print(soup1.p.children)
for i, child in enumerate(soup1.p.children):
    print(i, child)
<list_iterator object at 0x000000DD59609898>
0
            Once upon a time there were three little sisters; and their names were

1 <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
2 

3 <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
4
            and

5 <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
6
            and they lived at the bottom of a well.
        
from bs4 import BeautifulSoup

soup2 = BeautifulSoup(html, 'lxml')
print(soup2.p.children)
for i, desc in enumerate(soup2.p.descendants):
    print(i, desc)
<list_iterator object at 0x000000DD595897B8>
0
            Once upon a time there were three little sisters; and their names were

1 <a class="sister" href="http://example.com/elsie" id="link1">
<span>Elsie</span>
</a>
2 

3 <span>Elsie</span>
4 Elsie
5 

6 

7 <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
8 Lacie
9
            and

10 <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
11 Tillie
12
            and they lived at the bottom of a well.
        

继续潜水,后期再来丰富吧,记录一下

原文地址:https://www.cnblogs.com/wyy1480/p/11192153.html

时间: 2024-07-30 21:20:12

子标签和后代标签: .children 和 .descendants的相关文章

css基础 给一个标记的后代中 指定的标签 或者指定标签的指定class名称 添加样式 (后代多级的)

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ex1: code: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&

操作标签里的标签

遇到多个li标签内分别有多个a标签该如何操作到每一个a标签 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>操作标签里的标签</title> <script type="text/javascript" src="../js/jquery-1.7.1.min.js&qu

学会怎样使用Jsp 内置标签、jstl标签库及自定义标签

学习jsp不得不学习jsp标签,一般来说,对于一个jsp开发者,可以理解为jsp页面中出现的java代码越少,对jsp的掌握就越好,而替换掉java代码的重要方式就是使用jsp标签. jsp标签的分类: 1)内置标签(动作标签): 不需要在jsp页面导入标签 2)jstl标签: 需要在jsp页面中导入标签 3)自定义标签 : 开发者自行定义,需要在jsp页面导入标签 1.内置标签(动作标签): <jsp:forward />转发标签: 语法:<jsp:forward page="

web day13 JSTL标签库(c标签,自定义标签),MVC设计模式,JavaWeb经典三层框架

JSTL 1. jstl的概述 *apache的东西,依赖EL * 使用jstl需要导入jstl1.2.jar * 四大库: > core:核心库,重点 > fmt:格式化:日期.数字 > sql:过时 > xml:过时 2. 导入标签库 *jar包 * 在jsp页面中:<%@taglib prefix="前缀" uri="路径"%> ---------------------- core --> c标签! 1. out和s

?Highmaps网页图表教程之绘图区显示标签显示数据标签定位

?Highmaps网页图表教程之绘图区显示标签显示数据标签定位 Highmaps数据标签定位 由于数据标签是和节点一一对应,所以数据标签是依据节点位置进行定位的.本节详细讲解如何对数据标签进行定位. Highmaps节点定位 在mappoint.mapbubble.mapline图表类型,节点都有明确位置.而在map图表类型中,节点对应的是一个区域.所以,map图表类型的节点中心位置就可以由用户来设定.设置时候需要使用到以下两个配置项: middleX: Number1 middleY: Num

03-body标签中相关标签

今日主要内容: 列表标签 <ul>.<ol>.<dl> 表格标签 <table> 表单标签 <fom> 一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,"无序列表"的意思. li:list item,"列表项"的意思. 示例: <ul> <li>张三</li&g

一、JSP标签介绍,自定义标签

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 16.0000pt } h1 { margin-top: 17.0000pt; margin-bottom: 16.5000pt; margin-left: 21.6000pt; text-indent: -21.6000pt; page-break-after: avoid; page-

03-body标签中相关标签-2

主要内容: 列表标签 <ul>.<ol>.<dl>表格标签 <table>表单标签 <form> 一.列表标签 列表标签分为三种. 无序列表 有序列表 定义列表 1.无序列表<ul>,无序列表中的每一项是<li> 1.1.英文单词解释如下:ul:unordered list,"无序列表"的意思.li:list item,"列表项"的意思. 示例: <ul> <li&

前端之body标签中相关标签(二)

一 列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: a.ul:unordered list,“无序列表”的意思. b.li:list item,“列表项”的意思. 示例: <ul> <li>张三</li> <li>李四</li> <li>王五</li> </ul> 效果: 注意: a.li不能单独存在,必须包裹在ul里面:反过来说,u