- Beautiful Soup 教程
- Beautiful Soup - 首頁
- Beautiful Soup - 概述
- Beautiful Soup - 網頁抓取
- Beautiful Soup - 安裝
- Beautiful Soup - 解析頁面
- Beautiful Soup - 物件型別
- Beautiful Soup - 檢查資料來源
- Beautiful Soup - 抓取 HTML 內容
- Beautiful Soup - 透過標籤導航
- Beautiful Soup - 透過 ID 查詢元素
- Beautiful Soup - 透過 Class 查詢元素
- Beautiful Soup - 透過屬性查詢元素
- Beautiful Soup - 搜尋樹
- Beautiful Soup - 修改樹
- Beautiful Soup - 解析文件的一部分
- Beautiful Soup - 查詢元素的所有子元素
- Beautiful Soup - 使用 CSS 選擇器查詢元素
- Beautiful Soup - 查詢所有註釋
- Beautiful Soup - 從 HTML 中抓取列表
- Beautiful Soup - 從 HTML 中抓取段落
- BeautifulSoup - 從 HTML 中抓取連結
- Beautiful Soup - 獲取所有 HTML 標籤
- Beautiful Soup - 獲取標籤內部的文字
- Beautiful Soup - 查詢所有標題
- Beautiful Soup - 提取標題標籤
- Beautiful Soup - 提取電子郵件 ID
- Beautiful Soup - 抓取巢狀標籤
- Beautiful Soup - 解析表格
- Beautiful Soup - 選擇第 n 個子元素
- Beautiful Soup - 搜尋標籤內部的文字
- Beautiful Soup - 刪除 HTML 標籤
- Beautiful Soup - 刪除所有樣式
- Beautiful Soup - 刪除所有指令碼
- Beautiful Soup - 刪除空標籤
- Beautiful Soup - 刪除子元素
- Beautiful Soup - find 與 find_all 的區別
- Beautiful Soup - 指定解析器
- Beautiful Soup - 比較物件
- Beautiful Soup - 複製物件
- Beautiful Soup - 獲取標籤位置
- Beautiful Soup - 編碼
- Beautiful Soup - 輸出格式化
- Beautiful Soup - 美化輸出
- Beautiful Soup - NavigableString 類
- Beautiful Soup - 將物件轉換為字串
- Beautiful Soup - 將 HTML 轉換為文字
- Beautiful Soup - 解析 XML
- Beautiful Soup - 錯誤處理
- Beautiful Soup - 故障排除
- Beautiful Soup - 移植舊程式碼
- Beautiful Soup - 函式參考
- Beautiful Soup - contents 屬性
- Beautiful Soup - children 屬性
- Beautiful Soup - string 屬性
- Beautiful Soup - strings 屬性
- Beautiful Soup - stripped_strings 屬性
- Beautiful Soup - descendants 屬性
- Beautiful Soup - parent 屬性
- Beautiful Soup - parents 屬性
- Beautiful Soup - next_sibling 屬性
- Beautiful Soup - previous_sibling 屬性
- Beautiful Soup - next_siblings 屬性
- Beautiful Soup - previous_siblings 屬性
- Beautiful Soup - next_element 屬性
- Beautiful Soup - previous_element 屬性
- Beautiful Soup - next_elements 屬性
- Beautiful Soup - previous_elements 屬性
- Beautiful Soup - find 方法
- Beautiful Soup - find_all 方法
- Beautiful Soup - find_parents 方法
- Beautiful Soup - find_parent 方法
- Beautiful Soup - find_next_siblings 方法
- Beautiful Soup - find_next_sibling 方法
- Beautiful Soup - find_previous_siblings 方法
- Beautiful Soup - find_previous_sibling 方法
- Beautiful Soup - find_all_next 方法
- Beautiful Soup - find_next 方法
- Beautiful Soup - find_all_previous 方法
- Beautiful Soup - find_previous 方法
- Beautiful Soup - select 方法
- Beautiful Soup - append 方法
- Beautiful Soup - extend 方法
- Beautiful Soup - NavigableString 方法
- Beautiful Soup - new_tag 方法
- Beautiful Soup - insert 方法
- Beautiful Soup - insert_before 方法
- Beautiful Soup - insert_after 方法
- Beautiful Soup - clear 方法
- Beautiful Soup - extract 方法
- Beautiful Soup - decompose 方法
- Beautiful Soup - replace_with 方法
- Beautiful Soup - wrap 方法
- Beautiful Soup - unwrap 方法
- Beautiful Soup - smooth 方法
- Beautiful Soup - prettify 方法
- Beautiful Soup - encode 方法
- Beautiful Soup - decode 方法
- Beautiful Soup - get_text 方法
- Beautiful Soup - diagnose 方法
- Beautiful Soup 有用資源
- Beautiful Soup - 快速指南
- Beautiful Soup - 有用資源
- Beautiful Soup - 討論
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>