
- 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 - 透過類查詢元素
- 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 - 解析文件的一部分
假設您想使用 Beautiful Soup 僅檢視文件的 <a> 標籤。通常,您會解析樹並使用 find_all() 方法,並使用所需的標籤作為引數。
soup = BeautifulSoup(fp, "html.parser") tags = soup.find_all('a')
但這將非常耗時,並且會不必要地佔用更多記憶體。相反,您可以建立一個 SoupStrainer 類的物件,並將其用作 BeautifulSoup 建構函式的 parse_only 引數的值。
SoupStrainer 告訴 BeautifulSoup 要提取哪些部分,並且解析樹僅包含這些元素。如果您將所需資訊縮小到 HTML 的特定部分,這將加快搜索結果的速度。
product = SoupStrainer('div',{'id': 'products_list'}) soup = BeautifulSoup(html,parse_only=product)
以上程式碼行將僅從產品站點解析標題,這些標題可能位於標籤欄位內。
類似地,就像上面一樣,我們可以使用其他 soupStrainer 物件從 HTML 標籤中解析特定資訊。以下是一些示例 -
示例
from bs4 import BeautifulSoup, SoupStrainer #Only "a" tags only_a_tags = SoupStrainer("a") #Will parse only the below mentioned "ids". parse_only = SoupStrainer(id=["first", "third", "my_unique_id"]) soup = BeautifulSoup(my_document, "html.parser", parse_only=parse_only) #parse only where string length is less than 10 def is_short_string(string): return len(string) < 10 only_short_strings = SoupStrainer(string=is_short_string)
SoupStrainer 類採用與來自“搜尋樹”的典型方法相同的引數:name、attrs、text 和 **kwargs。
請注意,如果您使用 html5lib 解析器,此功能將不起作用,因為在這種情況下將解析整個文件,無論如何。因此,您應該使用內建的 html.parser 或 lxml 解析器。
您還可以將 SoupStrainer 傳遞到“搜尋樹”中介紹的任何方法中。
from bs4 import SoupStrainer a_tags = SoupStrainer("a") soup = BeautifulSoup(html_doc, 'html.parser') soup.find_all(a_tags)
廣告