【爬虫】2.4 BeautifulSoup遍历文档元素

1. 获取元素结点的父结点(上行遍历)


BeautifulSoup 通过tag.parent 获取 tag 结点的父结点,其中根结点<html>的父结点是名称为 [document] 的结点,这个 [document] 结点的父结点是 None

标签树的上行遍历

属性

说明

.parent

节点的父亲标签

.parents

节点先辈标签的迭代类型,用于循环遍历先辈节点


frombs4importBeautifulSoup
​
doc='''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">
Once upon a time there were three little sisters; and their names were
<a href="https://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="https://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="https://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
</body>
</html>
'''
​
# 找出文档中 <p class="title"><b>The Dormouse's story</b></p> 的<b>元素结点的所有父结点的名称
soup=BeautifulSoup(doc, "lxml")
print(soup.name)  # [document]
tag=soup.find("b")
whiletag:
    print(tag.name, end=" ")  # b p body html [document]
    tag=tag.parent
​

2. 获取元素结点的直接子元素结点(下行遍历)


BeautifulSoup 通过:tag.children 获取 tag 结点的所有直接子结点,包括element、text 等类型的结点。

标签树的下行遍历

属性

说明

.contents

子节点的列表,将<tag>所有儿子节点存入列表

.children

子节点的迭代类型,与.contents类似,用于循环遍历儿子节点

.descendants

子孙节点的迭代类型,包含所有子孙节点,用于循环遍历


frombs4importBeautifulSoup
​
doc='''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The<i>Dormouse's</i>story</b> Once upon a time...</p>
</body>
</html>
'''
​
# 获取<p>元素的所有直接子元素结点
soup=BeautifulSoup(doc, "lxml")
tag=soup.find("p")
foriintag.children:
    print(i)
    # <b>The<i>Dormouse's</i>story</b>
    #  Once upon a time...
​

3. 获取元素结点的所有子孙元素结点(下行遍历)


BeautifulSoup 通过:tag.desendants 获取 tag 结点的所有子孙结点元素,包括element 、text 等类型的结点


frombs4importBeautifulSoup
​
doc='''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The<i>Dormouse's</i>story</b> Once upon a time...</p>
</body>
</html>
'''
​
# 获取<p>元素的所有子孙元素结点
soup=BeautifulSoup(doc, "lxml")
tag=soup.find("p")
foriintag.descendants:
    print(i)
    # <b>The<i>Dormouse's</i>story</b>
    # The
    # <i>Dormouse's</i>
    # Dormouse's
    # story
    #  Once upon a time...
​

4. 获取元素结点的兄弟结点(平行遍历)


BeautifulSoup 通过...来获取...结点

tag.next_sibling (临近的)下一个兄弟结点
tag.previous_sibling (临近的) 前一个兄弟结点

标签树的平行遍历

属性

说明

.next_sibling

返回按照HTML文本顺序的下一个平行节点标签

.previous_sibling

返回按照HTML文本顺序的上一个平行节点标签

.next_siblings

迭代类型,返回按照HTML文本顺序的后续所有平行节点标签

.previous_siblings

迭代类型,返回按照HTML文本顺序的前续所有平行节点标签


frombs4importBeautifulSoup
​
doc='''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The<i>Dormouse's</i>story</b>Once upon a time...</p>
</body>
</html>
'''
​
# 查找前后兄弟结点
soup=BeautifulSoup(doc, "lxml")
tag=soup.find("b")
print(tag.previous_sibling)  # None
print(tag.next_sibling)  # Once upon a time...
tag=soup.find("i")
print(tag.previous_sibling)  # The
print(tag.next_sibling)  # story
​

下一篇文章:2.5 BeautifulSoup使用 CSS 语法查找元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值