1. 程式人生 > >爬蟲庫之BeautifulSoup學習(三)

爬蟲庫之BeautifulSoup學習(三)

子節點 rom lac repr 文檔 strong 爬蟲 time contents

遍歷文檔樹:

  1、查找子節點

  .contents  

  tag的.content屬性可以將tag的子節點以列表的方式輸出。

  print soup.body.contents

  print type(soup.body.contents)

  運行結果:

[u‘\n‘, <p class="title" name="dromouse"><b>The Dormouse‘s story</b></p>, u‘\n‘, <p class="story">Once upon a time there were three little sisters; and their names were\n<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,\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.</p>, u‘\n‘, <p class="story">...</p>, u‘\n‘]

<type ‘list‘>
[Finished in 0.2s]

.children

它返回的不是一個list,不過我們可以通過它來遍歷獲取所有子節點。

我們可以打印輸出,可以發現它返回的是一個list生成器對象

print soup.body.children

<listiterator object at 0x0294DE90>

我們怎樣獲得裏面的內容呢?遍歷一下就ok了:

for child in soup.boyd.children:

  print child

運行返回內容:

<p class="title" name="dromouse"><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" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;

and they lived at the bottom of a well.</p>


<p class="story">...</p>


[Finished in 0.2s]

2、所有子孫節點

.descendants

.contents 和 .children 屬性僅包含tag的直接子節點,.descendants 屬性可以對所有tag的子孫節點進行遞歸循環,和 children類似,我們也需要遍歷獲取其中的內容。

for child in soup.descendants:
  print child

運行結果如下,可以發現,所有的節點都被打印出來了,先生最外層的 HTML標簽,其次從 head 標簽一個個剝離,以此類推。

3、節點內容

.string

如果一個標簽裏面沒有標簽了,那麽 .string 就會返回標簽裏面的內容。如果標簽裏面只有唯一的一個標簽了,那麽 .string 也會返回最裏面的內容。

果tag包含了多個子節點,tag就無法確定,string 方法應該調用哪個子節點的內容, .string 的輸出結果是 None

print soup.head.string
print soup.title.string
print soup.body.string

#The Dormouse‘s story
#The Dormouse‘s story
#None
[Finished in 0.2s]

4、多個內容

.strings

獲取多個內容,不過需要遍歷獲取

for string in soup.strings:

  print repr(string)

  .stripped_strings

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

for string in soup.stripped_strings:
  print repr(string)

運行結果:

u"The Dormouse‘s story"
u"The Dormouse‘s story"
u‘Once upon a time there were three little sisters; and their names were‘
u‘,‘
u‘Lacie‘
u‘and‘
u‘Tillie‘
u‘;\nand they lived at the bottom of a well.‘
u‘...‘
[Finished in 0.2s]

5、父節點

.parent

print soup.p.parent.name

print soup.head.title.string.parent.name

#body

#title

6、兄弟節點、前後節點等略

爬蟲庫之BeautifulSoup學習(三)