Beautiful Soup - 使用 CSS 選擇器查詢元素



在 Beautiful Soup 庫中,select() 方法是用於抓取 HTML/XML 文件的重要工具。與 find() 和其他 find_*() 方法類似,select() 方法也有助於定位滿足給定條件的元素。但是,find*() 方法根據標籤名稱及其屬性搜尋 PageElements,而 select() 方法根據給定的 CSS 選擇器搜尋文件樹。

Beautiful Soup 還具有 select_one() 方法。select() 和 select_one() 的區別在於,select() 返回屬於 PageElement 並以 CSS 選擇器為特徵的所有元素的結果集;而 select_one() 返回滿足基於 CSS 選擇器選擇標準的元素的第一個匹配項。

在 Beautiful Soup 4.7 版本之前,select() 方法只能支援常用的 CSS 選擇器。隨著 4.7 版本的釋出,Beautiful Soup 集成了 Soup Sieve CSS 選擇器庫。因此,現在可以使用更多選擇器。在 4.12 版本中,除了現有的便利方法 select() 和 select_one() 之外,還添加了一個 .css 屬性。select() 方法的引數如下:

select(selector, limit, **kwargs)

selector − 包含 CSS 選擇器的字串。

limit − 找到此數量的結果後,停止查詢。

kwargs − 要傳遞的關鍵字引數。

如果 limit 引數設定為 1,則它等效於 select_one() 方法。select() 方法返回 Tag 物件的結果集,而 select_one() 方法返回單個 Tag 物件。

Soup Sieve 庫

Soup Sieve 是一個 CSS 選擇器庫。它已與 Beautiful Soup 4 整合,因此與 Beautiful Soup 包一起安裝。它提供了使用現代 CSS 選擇器選擇、匹配和過濾文件樹標籤的功能。Soup Sieve 目前實現了從 CSS 級別 1 規範到 CSS 級別 4 的大多數 CSS 選擇器,除了某些尚未實現的選擇器。

Soup Sieve 庫具有不同型別的 CSS 選擇器。基本的 CSS 選擇器包括:

型別選擇器

透過節點名稱匹配元素。例如:

tags = soup.select('div')

示例

from bs4 import BeautifulSoup, NavigableString

markup = '''
   <div id="Languages">
      <p>Java</p> <p>Python</p> <p>C++</p>
   </div>
'''
soup = BeautifulSoup(markup, 'html.parser')

tags = soup.select('div')
print (tags)

輸出

[<div id="Languages">
<p>Java</p> <p>Python</p> <p>C++</p>
</div>]

通用選擇器 (*)

它匹配任何型別的元素。例如:

tags = soup.select('*')

ID 選擇器

它根據元素的 id 屬性匹配元素。符號 # 表示 ID 選擇器。例如:

tags = soup.select("#nm")

示例

from bs4 import BeautifulSoup

html = '''
   <form>
      <input type = 'text' id = 'nm' name = 'name'>
      <input type = 'text' id = 'age' name = 'age'>
      <input type = 'text' id = 'marks' name = 'marks'>
   </form>
'''
soup = BeautifulSoup(html, 'html.parser')
obj = soup.select("#nm")
print (obj)

輸出

[<input id="nm" name="name" type="text"/>]

類選擇器

它根據 class 屬性中包含的值匹配元素。以 . 為字首的類名稱是 CSS 類選擇器。例如:

tags = soup.select(".submenu")

示例

from bs4 import BeautifulSoup, NavigableString

markup = '''
   <div id="Languages">
      <p>Java</p> <p>Python</p> <p>C++</p>
   </div>
'''
soup = BeautifulSoup(markup, 'html.parser')

tags = soup.select('div')
print (tags)

輸出

[<div id="Languages">
<p>Java</p> <p>Python</p> <p>C++</p>
</div>]

屬性選擇器

屬性選擇器根據元素的屬性匹配元素。

soup.select('[attr]')

示例

from bs4 import BeautifulSoup

html = '''
   <h1>Tutorialspoint Online Library</h1>
   <p><b>It's all Free</b></p>
   <a class="prog" href="https://tutorialspoint.tw/java/java_overview.htm" id="link1">Java</a> 
   <a class="prog" href="https://tutorialspoint.tw/cprogramming/index.htm" id="link2">C</a>
'''
soup = BeautifulSoup(html, 'html5lib')
print(soup.select('[href]'))

輸出

[<a class="prog" href="https://tutorialspoint.tw/java/java_overview.htm" id="link1">Java</a>, <a class="prog" href="https://tutorialspoint.tw/cprogramming/index.htm" id="link2">C</a>]

偽類

CSS 規範定義了許多偽 CSS 類。偽類是新增到選擇器中的關鍵字,用於定義所選元素的特殊狀態。它為現有元素新增效果。例如,:link 選擇尚未訪問的連結(每個 <a> 和 <area> 元素,且具有 href 屬性)。

偽類選擇器 nth-of-type 和 nth-child 被廣泛使用。

:nth-of-type()

選擇器 :nth-of-type() 根據其在一個兄弟元素組中的位置匹配給定型別的元素。關鍵字 even 和 odd 將分別從兄弟元素的子組中選擇元素。

在以下示例中,選擇了 <p> 型別的第二個元素。

示例

from bs4 import BeautifulSoup

html = '''
<p id="0"></p>
<p id="1"></p>
<span id="2"></span>
<span id="3"></span>
'''
soup = BeautifulSoup(html, 'html5lib')
print(soup.select('p:nth-of-type(2)'))

輸出

[<p id="1"></p>]

:nth-child()

此選擇器根據其在一個兄弟元素組中的位置匹配元素。關鍵字 even 和 odd 將分別選擇其位置在兄弟元素組中為偶數或奇數的元素。

用法

:nth-child(even)
:nth-child(odd)
:nth-child(2)

示例

from bs4 import BeautifulSoup, NavigableString

markup = '''
   <div id="Languages">
      <p>Java</p> <p>Python</p> <p>C++</p>
   </div>
'''
soup = BeautifulSoup(markup, 'html.parser')
tag = soup.div

child = tag.select_one(':nth-child(2)')
print (child)

輸出

<p>Python</p>
廣告

© . All rights reserved.