使用Python爬蟲庫BeautifulSoup遍歷文檔樹並對標簽進行操作詳解(新手必學)


為大家介紹下Python爬蟲庫BeautifulSoup遍歷文檔樹並對標簽進行操作的詳細方法與函數
下面就是使用Python爬蟲庫BeautifulSoup對文檔樹進行遍歷並對標簽進行操作的實例,都是最基礎的內容

需要代碼的同學可以添加群624440745

不懂的問題有老司機解決里面還有最新Python教程項目可拿,,一起相互監督共同進步!

html_doc = """
<html><head><title>The Dormouse's story</title></head>

<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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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,'lxml')

一、子節點

一個Tag可能包含多個字符串或者其他Tag,這些都是這個Tag的子節點.BeautifulSoup提供了許多操作和遍歷子結點的屬性。

1.通過Tag的名字來獲得Tag

print(soup.head)
print(soup.title)
1
2
<head><title>The Dormouse's story</title></head>
<title>The Dormouse's story</title>
1
2
通過名字的方法只能獲得第一個Tag,如果要獲得所有的某種Tag可以使用find_all方法

soup.find_all('a')
1
[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

2.contents屬性:將Tag的子節點通過列表的方式返回

head_tag = soup.head
head_tag.contents
1
2
[<title>The Dormouse's story</title>]
1
title_tag = head_tag.contents[0]
title_tag
1
2
<title>The Dormouse's story</title>
1
title_tag.contents
1
["The Dormouse's story"]
1
3.children:通過該屬性對子節點進行循環

for child in title_tag.children:
print(child)
1
2
The Dormouse's story
1
4.descendants: 不論是contents還是children都是返回直接子節點,而descendants對所有tag的子孫節點進行遞歸循環

for child in head_tag.children:
print(child)

```bash

```
for child in head_tag.descendants:
print(child)

<title>The Dormouse's story</title>
The Dormouse's story

5.string 如果tag只有一個NavigableString類型的子節點,那么tag可以使用.string得到該子節點

title_tag.string
1
"The Dormouse's story"
1
如果一個tag只有一個子節點,那么使用.string可以獲得其唯一子結點的NavigableString.

head_tag.string
1
head_tag.string
1
如果tag有多個子節點,tag無法確定.string對應的是那個子結點的內容,故返回None
print(soup.html.string)

None
1
6.strings和stripped_strings

如果tag包含多個字符串,可以使用.strings循環獲取

for string in soup.strings:
print(string)
1
2
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
;
and they lived at the bottom of a well.


...

.string輸出的內容包含了許多空格和空行,使用strpped_strings去除這些空白內容

for string in soup.stripped_strings:
print(string)
1
2
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
;
and they lived at the bottom of a well.
...

二、父節點

1.parent:獲得某個元素的父節點

title_tag = soup.title
title_tag.parent
1
2
<head><title>The Dormouse's story</title></head>
1
字符串也有父節點

title_tag.string.parent
1
<title>The Dormouse's story</title>
1
2.parents:遞歸的獲得所有父輩節點

link = soup.a
for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)

p
body
html
[document]

三、兄弟結點

sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>",'lxml')
print(sibling_soup.prettify())
1
2
<html>
<body>
<a>
<b>
text1
</b>
<c>
text2
</c>
</a>
</body>
</html>

1.next_sibling和previous_sibling

sibling_soup.b.next_sibling
1
<c>text2</c>
1
sibling_soup.c.previous_sibling
1
<b>text1</b>
1
在實際文檔中.next_sibling和previous_sibling通常是字符串或者空白符

soup.find_all('a')
1
[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

soup.a.next_sibling # 第一個<a></a>的next_sibling是,\n

```bash

‘,\n’


```bash
soup.a.next_sibling.next_sibling

<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
1
2.next_siblings和previous_siblings

for sibling in soup.a.next_siblings:
print(repr(sibling))
1
2
',\n'
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
' and\n'
<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>
';\nand they lived at the bottom of a well.'

for sibling in soup.find(id="link3").previous_siblings:
print(repr(sibling))
1
2
' and\n'
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
',\n'
<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>
'Once upon a time there were three little sisters; and their names were\n'

四、回退與前進

1.next_element和previous_element

指向下一個或者前一個被解析的對象(字符串或tag),即深度優先遍歷的后序節點和前序節點

last_a_tag = soup.find("a", id="link3")
print(last_a_tag.next_sibling)
print(last_a_tag.next_element)

;

and they lived at the bottom of a well.
Tillie
1
2
last_a_tag.previous_element
1
' and\n'
1
2.next_elements和previous_elements

通過.next_elements和previous_elements可以向前或向后訪問文檔的解析內容,就好像文檔正在被解析一樣

for element in last_a_tag.next_elements:
print(repr(element))
1
2
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
<p class="story">...</p>
'...'
'\n'
————————————————
注意:很多人學Python過程中會遇到各種煩惱問題解決不了。為此小編建了個Python全棧免費答疑交流.裙 :624440745,不懂的問題有老司機解決里面還有最新Python教程項目可拿,,一起相互監督共同進步!
本文的文字及圖片來源於網絡加上自己的想法,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM