Beautiful Soup4學(xué)習(xí)筆記(三):遍歷文檔樹

還是之前的字符串作為栗子:

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  class="sister" id="link1">Elsie</a>,
<a  class="sister" id="link2">Lacie</a> and
<a  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')

通過這段例子來演示怎樣從文檔的一段內(nèi)容找到另一段內(nèi)容

子節(jié)點(diǎn)

一個(gè)Tag可能包含多個(gè)字符串或其他的Tag,這些都是這個(gè)Tag的子節(jié)點(diǎn)。Beautiful Soup提供了許多操作和遍歷子節(jié)點(diǎn)的屬性.

注意: Beautiful Soup中字符串節(jié)點(diǎn)不支持這些屬性,因?yàn)樽址疀]有子節(jié)點(diǎn)

tag的名字

操作文檔樹最簡單的方法就是告訴它你想獲取的tag的name.如果想獲取 <head> 標(biāo)簽,只要用 soup.head :

>>> soup.head
<head><title>The Dormouse's story</title></head>
>>> soup.title
<title>The Dormouse's story</title>

這是個(gè)獲取tag的小竅門,可以在文檔樹的tag中多次調(diào)用這個(gè)方法.下面的代碼可以獲取<body>標(biāo)簽中的第一個(gè)<b>標(biāo)簽:

>>> soup.body.b
<b>The Dormouse's story</b>

通過點(diǎn)取屬性的方式只能獲得當(dāng)前名字的第一個(gè)tag:

>>> soup.a
<a class="sister"  id="link1">Elsie</a>

如果想要得到所有的<a>標(biāo)簽,或是通過名字得到比一個(gè)tag更多的內(nèi)容的時(shí)候,就需要用到 Searching the tree 中描述的方法,比如: find_all()

>>> soup.find_all('a')
[<a class="sister"  id="link1">Elsie</a>, <a class="sister" href="http://example.com/l
acie" id="link2">Lacie</a>, <a class="sister"  id="link3">Tillie</a>]

.contents 和 .children

tag的.contents屬可以將tag的子節(jié)點(diǎn)以列表的方式輸出:

>>> head_tag = soup.head  
>>> head_tag
<head><title>The Dormouse's story</title></head>
>>> head_tag.contents
[<title>The Dormouse's story</title>]
>>> title_tag = head_tag.contents[0]  
>>> title_tag 
<title>The Dormouse's story</title>
>>> title_tag .contents
["The Dormouse's story"]

BeautifulSoup 對象本身一定會包含子節(jié)點(diǎn),也就是說<html>標(biāo)簽也是 BeautifulSoup 對象的子節(jié)點(diǎn):

len(soup.contents)
# 1
soup.contents[0].name
# u'html'

這里,我以字符串的形式操作,是有空格存在的。

字符串沒有 .contents屬性,因?yàn)樽址疀]有子節(jié)點(diǎn):

>>> text = title_tag.contents[0]  
>>> text.contents  
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    text.contents
  File "/usr/local/lib/python3.5/site-packages/bs4/element.py", line 730, in __getattr__
    self.__class__.__name__, attr))
AttributeError: 'NavigableString' object has no attribute 'contents'

通過tag的.children生成器,可以對tag的子節(jié)點(diǎn)進(jìn)行循環(huán):

>>> for child in title_tag.children: 
...     print(child)  
...      
...    
... 
The Dormouse's story

.descendants

.contents 和 .children 屬性僅包含tag的直接子節(jié)點(diǎn).例如,<head>標(biāo)簽只有一個(gè)直接子節(jié)點(diǎn)<title>

>>> head_tag.contents
[<title>The Dormouse's story</title>]

但是<title>標(biāo)簽也包含一個(gè)子節(jié)點(diǎn):字符串 “The Dormouse’s story”,這種情況下字符串 “The Dormouse’s story”也屬于<head>標(biāo)簽的子孫節(jié)點(diǎn). .descendants 屬性可以對所有tag的子孫節(jié)點(diǎn)進(jìn)行遞歸循環(huán) :

>>> for child in head_tag.descendants:
...     print(child)  
...       
<title>The Dormouse's story</title>
The Dormouse's story

上面的例子中, <head>標(biāo)簽只有一個(gè)子節(jié)點(diǎn),但是有2個(gè)子孫節(jié)點(diǎn):<head>節(jié)點(diǎn)和<head>的子節(jié)點(diǎn), BeautifulSoup 有一個(gè)直接子節(jié)點(diǎn)(<html>節(jié)點(diǎn)),卻有很多子孫節(jié)點(diǎn):

len(list(soup.children))
# 1
len(list(soup.descendants))
# 25

這里的還是和用字符串的不一樣,還是復(fù)制了文檔。

.string

如果tag只有一個(gè) NavigableString 類型子節(jié)點(diǎn),那么這個(gè)tag可以使用 .string 得到子節(jié)點(diǎn):

>>> title_tag.string 
"The Dormouse's story"

如果一個(gè)tag僅有一個(gè)子節(jié)點(diǎn),那么這個(gè)tag也可以使用 .string 方法,輸出結(jié)果與當(dāng)前唯一子節(jié)點(diǎn)的 .string 結(jié)果相同:

>>> head_tag.contents
[<title>The Dormouse's story</title>]
>>> head_tag.string 
"The Dormouse's story"

如果tag包含了多個(gè)子節(jié)點(diǎn),tag就無法確定 .string 方法應(yīng)該調(diào)用哪個(gè)子節(jié)點(diǎn)的內(nèi)容, .string 的輸出結(jié)果是 None :

>>> print(soup.html.string) 
None

.strings 和 stripped_strings

如果tag中包含多個(gè)字符串,可以使用 .strings來循環(huán)獲取:

>>> 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'

注意:看到了吧,多了兩個(gè)換行符和文檔相比

輸出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白內(nèi)容:

>>> 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.'
'...'

全部是空格的行會被忽略掉,段首和段末的空白會被刪除

父節(jié)點(diǎn)

在文檔樹中每個(gè)tag或字符串都有父節(jié)點(diǎn):即被包含在某個(gè)tag中

.parent

通過 .parent 屬性來獲取某個(gè)元素的父節(jié)點(diǎn).在文檔中,<head>標(biāo)簽是<title>標(biāo)簽的父節(jié)點(diǎn):

>>> title_tag = soup.title 
>>> title_tag 
<title>The Dormouse's story</title>
>>> title_tag .parent 
<head><title>The Dormouse's story</title></head>

文檔title的字符串也有父節(jié)點(diǎn):<title>標(biāo)簽

>>> title_tag.string.parent 
<title>The Dormouse's story</title>

文檔的頂層節(jié)點(diǎn)比如<html>的父節(jié)點(diǎn)是 BeautifulSoup 對象:

>>> html_tag = soup.html 
>>> type(html_tag.parent) 
<class 'bs4.BeautifulSoup'>

BeautifulSoup 對象的 .parent 是None:

>>> print(soup.parent) 
None

.parents

通過元素的 .parents 屬性可以遞歸得到元素的所有父輩節(jié)點(diǎn),下面的例子使用了 .parents 方法遍歷了<a>標(biāo)簽到根節(jié)點(diǎn)的所有節(jié)點(diǎn).

>>> link = soup.a 
>>> link 
<a class="sister"  id="link1">Elsie</a>
>>> for parent in link.parents: 
...     if parent is None:
...         print(parent) 
...     else:
...         print(parent.name) 
... 
p
body
html
[document]

兄弟節(jié)點(diǎn)

看一段簡單的栗子:

>>> sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>") 
>>> print(sibling_soup.prettify())
<html>
 <body>
  <a>
   <b>
    text1
   </b>
   <c>
    text2
   </c>
  </a>
 </body>
</html>

因?yàn)?lt;b>標(biāo)簽和<c>標(biāo)簽是同一層:他們是同一個(gè)元素的子節(jié)點(diǎn),所以<b>和<c>可以被稱為兄弟節(jié)點(diǎn).一段文檔以標(biāo)準(zhǔn)格式輸出時(shí),兄弟節(jié)點(diǎn)有相同的縮進(jìn)級別.在代碼中也可以使用這種關(guān)系.

.next_sibling 和 .previous_sibling

在文檔樹中,使用 .next_sibling 和 .previous_sibling 屬性來查詢兄弟節(jié)點(diǎn):

>>> sibling_soup.b.next_sibling
<c>text2</c>
>>> sibling_soup.c.previous_sibling
<b>text1</b>

<b>標(biāo)簽有 .next_sibling 屬性,但是沒有 .previous_sibling 屬性,因?yàn)?lt;b>標(biāo)簽在同級節(jié)點(diǎn)中是第一個(gè).同理,<c>標(biāo)簽有 .previous_sibling 屬性,卻沒有 .next_sibling 屬性:

>>> print(sibling_soup.b.previous_sibling) 
None
>>> print(sibling_soup.c.next_sibling) 
None

例子中的字符串“text1”和“text2”不是兄弟節(jié)點(diǎn),因?yàn)樗鼈兊母腹?jié)點(diǎn)不同:

>>> sibling_soup.b.string
'text1'
>>> print(sibling_soup.b.string.next_sibling) 
None

實(shí)際文檔中的tag的 .next_sibling 和 .previous_sibling 屬性通常是字符串或空白. 看看看文檔:

<a  class="sister" id="link1">Elsie</a>
<a  class="sister" id="link2">Lacie</a>
<a  class="sister" id="link3">Tillie</a>

如果以為第一個(gè)<a>標(biāo)簽的 .next_sibling 結(jié)果是第二個(gè)<a>標(biāo)簽,那就錯了,真實(shí)結(jié)果是第一個(gè)<a>標(biāo)簽和第二個(gè)<a>標(biāo)簽之間的頓號和換行符:

>>> link = soup.a  
>>> link 
<a class="sister"  id="link1">Elsie</a>
>>> link.next_sibling 
',\n'

第二個(gè)<a>標(biāo)簽是頓號的 .next_sibling 屬性:

>>> link.next_sibling.next_sibling
<a class="sister"  id="link2">Lacie</a>

.next_siblings 和 .previous_siblings

通過 .next_siblings 和 .previous_siblings 屬性可以對當(dāng)前節(jié)點(diǎn)的兄弟節(jié)點(diǎn)迭代輸出:

>>> for sibling in soup.a.next_siblings:
...     print(repr(sibling))
...      
...  
... 
',\n'
<a class="sister"  id="link2">Lacie</a>
' and\n'
<a class="sister"  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))
...     
... 
' and\n'
<a class="sister"  id="link2">Lacie</a>
',\n'
<a class="sister"  id="link1">Elsie</a>
'Once upon a time there were three little sisters; and their names were\n'

回退和前進(jìn)

看一下文檔:

<html><head><title>The Dormouse's story</title></head>
<p class="title"><b>The Dormouse's story</b></p>

HTML解析器把這段字符串轉(zhuǎn)換成一連串的事件: “打開<html>標(biāo)簽”,”打開一個(gè)<head>標(biāo)簽”,”打開一個(gè)<title>標(biāo)簽”,”添加一段字符串”,”關(guān)閉<title>標(biāo)簽”,”打開<p>標(biāo)簽”,等等.Beautiful Soup提供了重現(xiàn)解析器初始化過程的方法.

.next_element 和 .previous_element

.next_element 屬性指向解析過程中下一個(gè)被解析的對象(字符串或tag),結(jié)果可能與 .next_sibling 相同,但通常是不一樣的.

這是“愛麗絲”文檔中最后一個(gè)<a>標(biāo)簽,它的 .next_sibling
結(jié)果是一個(gè)字符串,當(dāng)前的解析過程因?yàn)橛龅搅?lt;a>標(biāo)簽而中斷了:

>>> last_a_tag = soup.find("a",id="link3") 
>>> last_a_tag
<a class="sister"  id="link3">Tillie</a>
>>> last_a_tag.next_sibling
';\nand they lived at the bottom of a well.'

但這個(gè)<a>標(biāo)簽的 .next_element 屬性結(jié)果是在<a>標(biāo)簽被解析之后的解析內(nèi)容,不是<a>標(biāo)簽后的句子部分,應(yīng)該是字符串”Tillie”:

>>> last_a_tag.next_element
'Tillie'

這是因?yàn)樵谠嘉臋n中,字符串“Tillie” 在分號前出現(xiàn),解析器先進(jìn)入<a>標(biāo)簽,然后是字符串“Tillie”,然后關(guān)閉</a>標(biāo)簽,然后是分號和剩余部分.分號與<a>標(biāo)簽在同一層級,但是字符串“Tillie”會被先解析.
.previous_element 屬性剛好與 .next_element 相反,它指向當(dāng)前被解析的對象的前一個(gè)解析對象:

last_a_tag.previous_element
# u' and\n'
last_a_tag.previous_element.next_element
# <a class="sister"  id="link3">Tillie</a>

.next_elements 和 .previous_elements

通過 .next_elements 和 .previous_elements 的迭代器就可以向前或向后訪問文檔的解析內(nèi)容,就好像文檔正在被解析一樣:

>>> 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'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容