1. 程式人生 > 程式設計 >Python中BeautifuSoup庫的用法使用詳解

Python中BeautifuSoup庫的用法使用詳解

BeautifulSoup簡介

Beautiful Soup是python的一個庫,最主要的功能是從網頁抓取資料。官方解釋如下:

Beautiful Soup提供一些簡單的、python式的函式用來處理導航、搜尋、修改分析樹等功能。它是一個工具箱,通過解析文件為使用者提供需要抓取的資料,因為簡單,所以不需要多少程式碼就可以寫出一個完整的應用程式。

Beautiful Soup自動將輸入文件轉換為Unicode編碼,輸出文件轉換為utf-8編碼。你不需要考慮編碼方式,除非文件沒有指定一個編碼方式,這時,Beautiful Soup就不能自動識別編碼方式了。然後,你僅僅需要說明一下原始編碼方式就可以了。

Beautiful Soup已成為和lxml、html6lib一樣出色的python直譯器,為使用者靈活地提供不同的解析策略或強勁的速度。

BeautifulSoup支援Python標準庫中的HTML解析器,還支援一些第三方的解析器,如果我們不安裝它,則 Python 會使用 Python預設的解析器,lxml 解析器更加強大,速度更快,推薦使用lxml 解析器。

1. 建立 BeautifulSoup 物件

首先匯入庫bs4 lxml requests

#encoding:UTF-8
from bs4 import BeautifulSoup
import lxml
import requests

使用官方字串來演示:

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse">The Dormouse's story</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" 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" 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>
"""

建立 beautifulsoup 物件:

soup = BeautifulSoup(html,'lxml') #建立 beautifulsoup 物件

還可以用本地 HTML 檔案來建立物件:

soup1 = BeautifulSoup(open('index.html')) #用本地 HTML 檔案來建立物件

列印一下 soup 物件的內容,格式化輸出:

print soup.prettify() #列印 soup 物件的內容,格式化輸出

輸出結果,格式化打印出了它的內容,這個函式經常用到。

2. 四種物件

Beautiful Soup將複雜HTML文件轉換成一個複雜的樹形結構,每個節點都是Python物件,所有物件可以歸納為4種:

  • Tag
  • NavigableString
  • BeautifulSoup
  • Comment

(1)Tag

Tag就是 HTML 中的一個個標籤,例如:

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

用 BeautifulSoup 可以很方便地獲取 Tags:

print soup.title
print soup.head
print soup.a
print soup.p
print type(soup.a)

這種方式查詢的是在所有內容中的第一個符合要求的標籤。

對於 Tag,它有兩個重要的屬性,name 和 attrs :

print soup.name
print soup.a.name
print soup.attrs
print soup.p.attrs #在這裡,我們把 p 標籤的所有屬性列印輸出了出來,得到的型別是一個字典。
print soup.p['class'] #單獨獲取某個屬性
print soup.p.get('class') ##單獨獲取某個屬性 跟上面一樣的

可以對這些屬性和內容等等進行修改:

soup.p['class']="newClass"

也可以對這個屬性進行刪除:

del soup.p['class']

(2)NavigableString

得到了標籤的內容用 .string 即可獲取標籤內部的文字,例如:

print soup.p.string

來檢查一下它的型別:

print type(soup.p.string)
#<class 'bs4.element.NavigableString'>

可以看到它的型別是一個NavigableString,翻譯過來叫 可以遍歷的字串。

(3)BeautifulSoup

BeautifulSoup物件表示的是一個文件的全部內容.大部分時候,可以把它當作Tag物件,是一個特殊的 Tag,我們可以分別獲取它的型別,名稱:

print type(soup.name)
#<type 'unicode'>
print soup.name 
# [document]
print soup.attrs 
#{} 空字典

(4)Comment

Comment物件是一個特殊型別的NavigableString物件,其實輸出的內容仍然不包括註釋符號。我們找一個帶註釋的標籤:

print soup.a
print soup.a.string
print type(soup.a.string)

執行結果如下:

<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>
 Elsie 
<class 'bs4.element.Comment'>

a 標籤裡的內容實際上是註釋,但是如果我們利用 .string 來輸出它的內容,我們發現它已經把註釋符號去掉了,所以這可能會給我們帶來不必要的麻煩。

我們列印輸出下它的型別,發現它是一個 Comment 型別,所以,我們在使用前最好做一下判斷,判斷程式碼如下:

if type(soup.a.string)==bs4.element.Comment:
  print soup.a.string

上面的程式碼中,我們判斷了它的型別,是否為 Comment 型別。

3. 遍歷文件樹

(1)直接子節點

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

print soup.head.contents 
#執行結果:
#[<title>The Dormouse's story</title>]
 
#輸出方式為列表,我們可以用列表索引來獲取它的某一個元素:
print soup.head.contents[0]

.children

它返回的不是一個 list,不過我們可以通過遍歷獲取所有子節點。我們列印輸出 .children 看一下,可以發現它是一個 list 生成器物件:

print soup.head.children
#執行結果:
#<listiterator object at 0x7f71457f5710>
 
#遍歷一下獲得裡面的內容:
for item in soup.body.children:
  print item

(2)所有子孫節點

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

for item in soup.descendants:
  print item
 
#檢視執行結果,可以發現,所有的節點都被打印出來了:
#<html><head><title>The Dormouse's story</title></head>
#<body>
#<p class="title" name="dromouse">The Dormouse's story</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>,

(3)節點內容

如果一個標籤裡面沒有標籤了,那麼 .string 就會返回標籤裡面的內容。如果標籤裡面只有唯一的一個標籤了,那麼 .string 也會返回最裡面的內容:

print soup.head.string
#執行結果:
#The Dormouse's story
 
#第二種情況:
print soup.title.string
#執行結果:
#The Dormouse's story

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

print soup.html.string
#執行結果:
#None

(4)多個內容

.strings 獲取多個內容,不過需要遍歷獲取,比如下面的例子:

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

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

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

(5)父節點

p = soup.p
print p.parent.name
#執行結果:
#body
 
content = soup.head.title.string
print content.parent.name
#執行結果:
#title

(6)全部父節點

通過元素的.parents屬性可以遞迴得到元素的所有父輩節點:

content = soup.head.title.string
for parent in content.parents:
  print parent.name

(7)兄弟節點

兄弟節點可以理解為和本節點處在統一級的節點,.next_sibling 屬性獲取了該節點的下一個兄弟節點,.previous_sibling 屬性獲取了該節點的上一個兄弟節點,如果節點不存在,則返回 None

注意:實際文件中的tag的 .next_sibling 和 .previous_sibling 屬性通常是字串或空白,因為空白或者換行也可以被視作一個節點,所以得到的結果可能是空白或者換行。

print soup.p.next_sibling
#    實際該處為空白
print soup.p.prev_sibling
#None  沒有前一個兄弟節點,返回 None
print soup.p.next_sibling.next_sibling

(8)全部兄弟節點

通過.next_siblings和.previous_siblings屬性可以對當前節點的兄弟節點迭代輸出:

for sibling in soup.a.next_siblings:
  print(repr(sibling))

(9)前後節點

與.next_sibling .previous_sibling 不同,它並不是針對於兄弟節點,而是在所有節點,不分層次

#比如head節點為:
<head><title>The Dormouse's story</title></head>
#那麼它的下一個節點便是 title,它是不分層次關係的。
print soup.head.next_element
#<title>The Dormouse's story</title>

(10)所有前後節點

通過.next_elements和.previous_elements的迭代器就可以向前或向後訪問文件的解析內容:

for element in last_a_tag.next_elements:
  print(repr(element))

4. 搜尋文件樹

(1)find_all( name,attrs,recursive,text,**kwargs )

find_all()方法搜尋當前tag的所有tag子節點,並判斷是否符合過濾器的條件

name 引數
A.傳字串

name引數可以查詢所有名字為name的tag,字串物件會被自動忽略掉

最簡單的過濾器是字串.在搜尋方法中傳入一個字串引數,Beautiful Soup會查詢與字串完整匹配的內容,下面的例子用於查詢文件中所有的標籤:

soup.find_all('b')
# [The Dormouse's story]

B.傳正則表示式

如果傳入正則表示式作為引數,Beautiful Soup會通過正則表示式的match()來匹配內容.下面例子中找出所有以b開頭的標籤,這表示<body>和標籤都應該被找到

import re
for tag in soup.find_all(re.compile("^b")):
  print(tag.name)
# body
# b

C.傳列表

如果傳入列表引數,Beautiful Soup會將與列表中任一元素匹配的內容返回.下面程式碼找到文件中所有<a>標籤和標籤:

soup.find_all(["a","b"])
# [The Dormouse's story,# <a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" 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" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

D.傳 True

True可以匹配任何值,下面程式碼查詢到所有的tag,但是不會返回字串節點:

for tag in soup.find_all(True):
  print(tag.name)
# html
# head
# title
# body
# p
# b
# p
# a
# a

E.傳方法

如果沒有合適過濾器,那麼還可以定義一個方法,方法只接受一個元素引數,如果這個方法返回True表示當前元素匹配並且被找到,如果不是則反回False。下面方法校驗了當前元素,如果包含class屬性卻不包含id屬性,那麼將返回True:

def has_class_but_no_id(tag):
  return tag.has_attr('class') and not tag.has_attr('id')

將這個方法作為引數傳入find_all()方法,將得到所有<p>標籤:

soup.find_all(has_class_but_no_id)
# [<p class="title">The Dormouse's story</p>,# <p class="story">Once upon a time there were...</p>,# <p class="story">...</p>]

2)keyword 引數

如果一個指定名字的引數不是搜尋內建的引數名,搜尋時會把該引數當作指定名字tag的屬性來搜尋,如果包含一個名字為id的引數,Beautiful Soup會搜尋每個tag的”id”屬性:

soup.find_all(id='link2')
# [<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" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>]

如果傳入href引數,Beautiful Soup會搜尋每個tag的”href”屬性:

soup.find_all(href=re.compile("elsie"))
# [<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>]

使用多個指定名字的引數可以同時過濾tag的多個屬性:

soup.find_all(href=re.compile("elsie"),id='link1')
# [<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">three</a>]

在這裡我們想用 class 過濾,不過 class 是 python 的關鍵詞,這怎麼辦?加個下劃線就可以:

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

有些tag屬性在搜尋不能使用,比如HTML5中的 data-* 屬性:

data_soup = BeautifulSoup('<div data-foo="value">foo!</div>')
data_soup.find_all(data-foo="value")
# SyntaxError: keyword can't be an expression

可以通過find_all()方法的attrs引數定義一個字典引數來搜尋包含特殊屬性的tag:

data_soup.find_all(attrs={"data-foo": "value"})
# [<div data-foo="value">foo!</div>]

3)text 引數

通過text引數可以搜搜文件中的字串內容.與name引數的可選值一樣,text引數接受 字串,正則表示式,列表,True:

soup.find_all(text="Elsie")
# [u'Elsie']
 
soup.find_all(text=["Tillie","Elsie","Lacie"])
# [u'Elsie',u'Lacie',u'Tillie']
 
soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story",u"The Dormouse's story"]

4)limit 引數

find_all()方法返回全部的搜尋結構,如果文件樹很大那麼搜尋會很慢.如果我們不需要全部結果,可以使用limit引數限制返回結果的數量.效果與SQL中的limit關鍵字類似,當搜尋到的結果數量達到limit的限制時,就停止搜尋返回結果.

文件樹中有3個tag符合搜尋條件,但結果只返回了2個,因為我們限制了返回數量:

soup.find_all("a",limit=2)
# [<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>]

5)recursive 引數

呼叫tag的find_all()方法時,Beautiful Soup會檢索當前tag的所有子孫節點,如果只想搜尋tag的直接子節點,可以使用引數recursive=False:

soup.html.find_all("title")
# [<title>The Dormouse's story</title>]
 
soup.html.find_all("title",recursive=False)

(2)find( name,**kwargs )

它與 find_all() 方法唯一的區別是find_all()方法的返回結果是值包含一個元素的列表,而find()方法直接返回結果

(3)find_parents() find_parent()

find_all()和find()只搜尋當前節點的所有子節點,孫子節點等.find_parents()和find_parent()用來搜尋當前節點的父輩節點,搜尋方法與普通tag的搜尋方法相同,搜尋文件搜尋文件包含的內容

(4)find_next_siblings() find_next_sibling()

這2個方法通過 .next_siblings 屬性對當 tag 的所有後面解析的兄弟 tag 節點進行迭代,find_next_siblings()方法返回所有符合條件的後面的兄弟節點,find_next_sibling()只返回符合條件的後面的第一個tag節點

(5)find_previous_siblings() find_previous_sibling()

這2個方法通過 .previous_siblings 屬性對當前 tag 的前面解析的兄弟 tag 節點進行迭代,find_previous_siblings()方法返回所有符合條件的前面的兄弟節點,find_previous_sibling()方法返回第一個符合條件的前面的兄弟節點

(6)find_all_next() find_next()

這2個方法通過 .next_elements 屬性對當前 tag 的之後的tag 和字串進行迭代,find_all_next()方法返回所有符合條件的節點,find_next()方法返回第一個符合條件的節點

(7)find_all_previous() 和 find_previous()

這2個方法通過 .previous_elements 屬性對當前節點前面的 tag 和字串進行迭代,find_all_previous()方法返回所有符合條件的節點,find_previous()方法返回第一個符合條件的節點

注:以上(2)(3)(4)(5)(6)(7)方法引數用法與 find_all() 完全相同,原理均類似。

5. CSS選擇器

我們在寫 CSS 時,標籤名不加任何修飾,類名前加點,id名前加 #,在這裡我們也可以利用類似的方法來篩選元素,用到的方法是soup.select(),返回型別是list

(1)通過標籤名查詢

print soup.select('title') 
#[<title>The Dormouse's story</title>]
 
print soup.select('a')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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" 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" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

(2)通過類名查詢

print soup.select('.sister')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>,<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

(3)通過 id 名查詢

print soup.select('#link1')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

(4)組合查詢

組合查詢即和寫 class 檔案時,標籤名與類名、id名進行的組合原理是一樣的,例如查詢 p 標籤中,id 等於 link1的內容,二者需要用空格分開:

print soup.select('p #link1')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

直接子標籤查詢:

print soup.select("head > title")
#[<title>The Dormouse's story</title>]

(5)屬性查詢

查詢時還可以加入屬性元素,屬性需要用中括號括起來,注意屬性和標籤屬於同一節點,所以中間不能加空格,否則會無法匹配到:

print soup.select('a[class="sister"]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>,<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
 
print soup.select('a[href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]
 
#屬性也可以與上述查詢方式組合,不在同一節點的空格隔開,同一節點的不加空格:
print soup.select('p a[href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]
 
#以上的 select 方法返回的結果都是列表形式,可以遍歷形式輸出,然後用 get_text() 方法來獲取它的內容:
soup = BeautifulSoup(html,'lxml')
print type(soup.select('title'))
print soup.select('title')[0].get_text()
 
for title in soup.select('title'):
  print title.get_text()

find_all和select的區別

find_all()對於屬性的值不是精確匹配的,是模糊匹配。同樣的,find()也是模糊匹配

select()方法使用css定位元素,根據td標籤和class屬性值 精確定位,但是會查詢出來所有符合條件的元素,返回一個列表

更詳細的內容檢視官方文件:http://beautifulsoup.readthedocs.io/zh_CN/latest/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。