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标签 # 获取head
print(soup.head)
复制 <head><title>The Dormouse's story</title></head>
复制 1.1.2 获取title # 获取title
print(soup.title)
复制 <title>The Dormouse's story</title>
复制 1.1.3 获取body
标签中的第一个b
标签 # 获取<body>标签中的第一个<b>标签
print(soup.body.b)
复制 <b>The Dormouse's story</b>
复制 1.1.4 获得当前名字的第一个tag # 获得当前名字的第一个tag
print(soup.a)
复制 <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
复制 1.1.5 获取所有a标签 # 获取所有a标签
print(soup.find_all('a'))
复制 [<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
子节点以列表的方式输出:# .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)
复制 <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
的子节点进行循环:# .children生成器,可以对tag的子节点进行循环
for child in title_tag.children:
print(child)
复制 The Dormouse's story
复制 1.3 .descendants .descendants
属性对所有tag
的子孙节点进行递归循环:for child in head_tag.descendants:
print(child)
复制 <title>The Dormouse's story</title>
The Dormouse's story
复制 1.4 .string 如果tag
只有一个 NavigableString
类型子节点,那么这个tag
可以使用 .string
得到子节点: # 如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点:
print(title_tag.string)
复制 The Dormouse's story
复制 1.5 .strings 和 stripped_strings 如果tag
中包含多个字符串,可以使用 .strings
来循环获取: for string in soup.strings:
print(repr(string))
复制 '\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
可以去除多余空白内容: # 使用 .stripped_strings 可以去除多余空白内容:
for string in soup.stripped_strings:
print(repr(string))
复制 "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
标签的父节点:# 通过 .parent 属性来获取某个元素的父节点,head标签是title标签的父节点:
title_tag = soup.title
print(title_tag)
print(title_tag.parent)
复制 <title>The Dormouse's story</title>
<head><title>The Dormouse's story</title></head>
复制 2.2 .parents 通过元素的 .parents 属性可以递归得到元素的所有父辈节点: link = soup.a
print(link)
for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)
复制 <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
p
body
html
[document]
复制 3 兄弟节点 两个标签是同一层,他们是同一个元素的子节点,则这两个标签是兄弟节点; 如下,b和c标签是兄弟节点: sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>")
print(sibling_soup.prettify())
复制 <a>
<b>
text1
</b>
<c>
text2
</c>
</a>
复制 3.1 .next_sibling 和 .previous_sibling 使用 .next_sibling
和 .previous_sibling
属性来查询兄弟节点: # 使用 .next_sibling 和 .previous_sibling 属性来查询兄弟节点:
print(sibling_soup.b.next_sibling)
print(sibling_soup.c.previous_sibling)
复制 <c>text2</c>
<b>text1</b>
复制 3.2 .next_siblings 和 .previous_siblings 通过 .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))
复制 ',\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
):last_a_tag = soup.find("a", id="link3")
print(last_a_tag)
print(last_a_tag.next_sibling)
复制 <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
相反,它指向当前被解析的对象的前一个解析对象:print(last_a_tag.previous_element)
print(last_a_tag.previous_element.next_element)
复制 and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
复制 4.2 .next_elements 和 .previous_elements 通过 .next_elements
和 .previous_elements
的迭代器就可以向前或向后访问文档的解析内容: for element in last_a_tag.next_elements:
print(repr(element))
复制 'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
<p class="story">...</p>
'...'
'\n'
复制 5 本为涉及的源码: # -*- 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))
复制