前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BeautifulSoup文档3-详细方法 | 如何对文档树进行遍历?

BeautifulSoup文档3-详细方法 | 如何对文档树进行遍历?

原创
作者头像
虫无涯
发布2023-02-22 09:12:06
5740
发布2023-02-22 09:12:06
举报
文章被收录于专栏:全栈测试技术全栈测试技术
  • 以下实例还是官网的例子:
代码语言:python
复制
html_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="http://example.com/elsie" class="sister" id="link1">Elsie</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
soup = BeautifulSoup(html_doc, 'html.parser')

1 子节点

1.1 tag的名字

1.1.1 获取head标签

代码语言:python
复制
# 获取head
print(soup.head)
  • 输出为:
代码语言:python
复制
<head><title>The Dormouse's story</title></head>

1.1.2 获取title

代码语言:python
复制
# 获取title
print(soup.title)
  • 输出为:
代码语言:python
复制
<title>The Dormouse's story</title>

1.1.3 获取body标签中的第一个b标签

代码语言:python
复制
# 获取<body>标签中的第一个<b>标签
print(soup.body.b)
  • 输出为:
代码语言:python
复制
<b>The Dormouse's story</b>

1.1.4 获得当前名字的第一个tag

代码语言:python
复制
# 获得当前名字的第一个tag
print(soup.a)
  • 输出为:
代码语言:python
复制
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

1.1.5 获取所有a标签

代码语言:python
复制
# 获取所有a标签
print(soup.find_all('a'))
  • 输出为:
代码语言:python
复制
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, 
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, 
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

1.2 .contents 和 .children

  • .contents 属性将tag子节点以列表的方式输出:
代码语言:python
复制
# .contents属性将`tag`子节点以列表的方式输出
head_tag = soup.head
print(head_tag)
print(head_tag.contents)
title_tag = head_tag.contents[0]
print(title_tag)
print(title_tag.contents)
  • 输出为:
代码语言:python
复制
<head><title>The Dormouse's story</title></head>
[<title>The Dormouse's story</title>]
<title>The Dormouse's story</title>
["The Dormouse's story"]
  • .children 生成器,可以对tag的子节点进行循环:
代码语言:python
复制
# .children生成器,可以对tag的子节点进行循环
for child in title_tag.children:
    print(child)
  • 输出为:
代码语言:python
复制
The Dormouse's story

1.3 .descendants

  • .descendants 属性对所有tag的子孙节点进行递归循环:
代码语言:python
复制
for child in head_tag.descendants:
    print(child)
  • 输出为:
代码语言:python
复制
<title>The Dormouse's story</title>
The Dormouse's story

1.4 .string

  • 如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点:
代码语言:python
复制
# 如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点:
print(title_tag.string)
  • 输出为:
代码语言:python
复制
The Dormouse's story

1.5 .strings 和 stripped_strings

  • 如果tag中包含多个字符串,可以使用 .strings 来循环获取:
代码语言:python
复制
for string in soup.strings:
    print(repr(string))
  • 输出为:
代码语言:python
复制
'\n'
"The Dormouse's story"
'\n'
'\n'
"The Dormouse's story"
'\n'
'Once upon a time there were three little sisters; and their names were\n'
'Elsie'
',\n'
'Lacie'
' and\n'
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
'...'
'\n'
  • 使用 .stripped_strings 可以去除多余空白内容:
代码语言:python
复制
# 使用 .stripped_strings 可以去除多余空白内容:
for string in soup.stripped_strings:
    print(repr(string))
  • 输出为:
代码语言:python
复制
"The Dormouse's story"
"The Dormouse's story"
'Once upon a time there were three little sisters; and their names were'
'Elsie'
','
'Lacie'
'and'
'Tillie'
';\nand they lived at the bottom of a well.'
'...'

2 父节点

2.1 .parent

  • 通过 .parent 属性来获取某个元素的父节点;
  • head标签是title标签的父节点:
代码语言:python
复制
#  通过 .parent 属性来获取某个元素的父节点,head标签是title标签的父节点:
title_tag = soup.title
print(title_tag)
print(title_tag.parent)
  • 输出为:
代码语言:python
复制
<title>The Dormouse's story</title>
<head><title>The Dormouse's story</title></head>

2.2 .parents

  • 通过元素的 .parents 属性可以递归得到元素的所有父辈节点:
代码语言:python
复制
link = soup.a
print(link)
for parent in link.parents:
    if parent is None:
        print(parent)
    else:
        print(parent.name)
  • 输出为:
代码语言:python
复制
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
p
body
html
[document]

3 兄弟节点

  • 两个标签是同一层,他们是同一个元素的子节点,则这两个标签是兄弟节点;
  • 如下,b和c标签是兄弟节点:
代码语言:python
复制
sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>")
print(sibling_soup.prettify())
  • 输出为:
代码语言:python
复制
<a>
 <b>
  text1
 </b>
 <c>
  text2
 </c>
</a>

3.1 .next_sibling 和 .previous_sibling

  • 使用 .next_sibling.previous_sibling 属性来查询兄弟节点:
代码语言:python
复制
# 使用 .next_sibling 和 .previous_sibling 属性来查询兄弟节点:
print(sibling_soup.b.next_sibling)
print(sibling_soup.c.previous_sibling)
  • 输出为:
代码语言:python
复制
<c>text2</c>
<b>text1</b>

3.2 .next_siblings 和 .previous_siblings

  • 通过 .next_siblings.previous_siblings 属性可以对当前节点的兄弟节点迭代输出:
代码语言:python
复制
for sibling in soup.a.next_siblings:
    print(repr(sibling))
for sibling in soup.find(id="link3").previous_siblings:
    print(repr(sibling))
  • 输出为:
代码语言:python
复制
',\n'
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
' and\n'
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
';\nand they lived at the bottom of a well.'
' and\n'
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
',\n'
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
'Once upon a time there were three little sisters; and their names were\n'

4 回退和前进

4.1 .next_element 和 .previous_element

  • .next_element 属性指向解析过程中下一个被解析的对象(字符串或tag):
代码语言:python
复制
last_a_tag = soup.find("a", id="link3")
print(last_a_tag)
print(last_a_tag.next_sibling)
  • 输出为:
代码语言:python
复制
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
;
and they lived at the bottom of a well.
  • .previous_element 属性刚好与 .next_element 相反,它指向当前被解析的对象的前一个解析对象:
代码语言:python
复制
print(last_a_tag.previous_element)
print(last_a_tag.previous_element.next_element)
  • 输出为:
代码语言:python
复制
 and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

4.2 .next_elements 和 .previous_elements

  • 通过 .next_elements.previous_elements 的迭代器就可以向前或向后访问文档的解析内容:
代码语言:python
复制
for element in last_a_tag.next_elements:
    print(repr(element))
  • 输出为:
代码语言:python
复制
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
<p class="story">...</p>
'...'
'\n'

5 本为涉及的源码:

代码语言:python
复制
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2023/2/16
# 文件名称:bs03.py
# 作用:BeautifulSoup的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

from bs4 import BeautifulSoup

html_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="http://example.com/elsie" class="sister" id="link1">Elsie</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>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

# ====== 子节点======
# 获取head
print(soup.head)

# 获取title
print(soup.title)

# 获取<body>标签中的第一个<b>标签
print(soup.body.b)

# 获得当前名字的第一个tag
print(soup.a)

# 获取所有a标签
print(soup.find_all('a'))

# .contents属性将`tag`子节点以列表的方式输出
head_tag = soup.head
print(head_tag)
print(head_tag.contents)
title_tag = head_tag.contents[0]
print(title_tag)
print(title_tag.contents)

# .children生成器,可以对tag的子节点进行循环
for child in title_tag.children:
    print(child)

# .descendants属性对所有tag的子孙节点进行递归循环
for child in head_tag.descendants:
    print(child)

# 如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点:
print(title_tag.string)

# 如果tag中包含多个字符串,可以使用 .strings来循环获取
for string in soup.strings:
    print(repr(string))

# 使用 .stripped_strings 可以去除多余空白内容:
for string in soup.stripped_strings:
    print(repr(string))


# ====== 父节点======
#  通过 .parent 属性来获取某个元素的父节点,head标签是title标签的父节点:
title_tag = soup.title
print(title_tag)
print(title_tag.parent)

# 通过元素的 .parents 属性可以递归得到元素的所有父辈节点
link = soup.a
print(link)
for parent in link.parents:
    if parent is None:
        print(parent)
    else:
        print(parent.name)


# ====== 兄弟节点======
sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>", 'html.parser')
print(sibling_soup.prettify())

# 使用 .next_sibling 和 .previous_sibling 属性来查询兄弟节点:
print(sibling_soup.b.next_sibling)
print(sibling_soup.c.previous_sibling)

# 通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出
for sibling in soup.a.next_siblings:
    print(repr(sibling))
for sibling in soup.find(id="link3").previous_siblings:
    print(repr(sibling))


# ====== 回退和前进======
# .next_element 属性指向解析过程中下一个被解析的对象(字符串或tag)
last_a_tag = soup.find("a", id="link3")
print(last_a_tag)
print(last_a_tag.next_sibling)

# .previous_element 属性刚好与 .next_element 相反,它指向当前被解析的对象的前一个解析对象
print(last_a_tag.previous_element)
print(last_a_tag.previous_element.next_element)

# 通过 .next_elements 和 .previous_elements 的迭代器就可以向前或向后访问文档的解析内容
for element in last_a_tag.next_elements:
    print(repr(element))

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 子节点
    • 1.1 tag的名字
      • 1.1.1 获取head标签
      • 1.1.2 获取title
      • 1.1.3 获取body标签中的第一个b标签
      • 1.1.4 获得当前名字的第一个tag
      • 1.1.5 获取所有a标签
    • 1.2 .contents 和 .children
      • 1.3 .descendants
        • 1.4 .string
          • 1.5 .strings 和 stripped_strings
          • 2 父节点
            • 2.1 .parent
              • 2.2 .parents
              • 3 兄弟节点
                • 3.1 .next_sibling 和 .previous_sibling
                  • 3.2 .next_siblings 和 .previous_siblings
                  • 4 回退和前进
                    • 4.1 .next_element 和 .previous_element
                      • 4.2 .next_elements 和 .previous_elements
                      • 5 本为涉及的源码:
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档