
- 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 vs 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 解析 HTML/XML 文件時,您可能會遇到錯誤,這些錯誤並非來自您的指令碼,而是來自程式碼片段的結構,因為 BeautifulSoup API 會丟擲錯誤。
預設情況下,BeautifulSoup 包會將文件解析為 HTML,但是,使用 beautifulsoup4 以非常優雅的方式處理格式錯誤的 XML 非常容易。
要將文件解析為 XML,您需要安裝 lxml 解析器,您只需要將“xml”作為第二個引數傳遞給 Beautifulsoup 建構函式:
soup = BeautifulSoup(markup, "lxml-xml")
或者
soup = BeautifulSoup(markup, "xml")
一個常見的 XML 解析錯誤是:
AttributeError: 'NoneType' object has no attribute 'attrib'
這可能會發生在使用 find() 或 findall() 函式時,某些元素丟失或未定義的情況下。
除了上述解析錯誤之外,您還可能遇到其他解析問題,例如環境問題,您的指令碼可能在一個作業系統中工作,但在另一個作業系統中不工作,或者在一個虛擬環境中工作,但在另一個虛擬環境中不工作,或者在虛擬環境外部不工作。所有這些問題可能是因為這兩個環境具有不同的解析器庫。
建議瞭解或檢查您當前工作環境中的預設解析器。您可以檢查當前工作環境中可用的當前預設解析器,或者顯式地將所需的解析器庫作為第二個引數傳遞給 BeautifulSoup 建構函式。
由於 HTML 標籤和屬性不區分大小寫,所有三個 HTML 解析器都會將標籤和屬性名稱轉換為小寫。但是,如果您想保留混合大小寫或大寫標籤和屬性,最好將文件解析為 XML。
UnicodeEncodeError
讓我們看一下下面的程式碼段:
示例
soup = BeautifulSoup(response, "html.parser") print (soup)
輸出
UnicodeEncodeError: 'charmap' codec can't encode character '\u011f'
上述問題可能是由於兩種主要情況造成的。您可能試圖打印出控制檯不知道如何顯示的 Unicode 字元。其次,您試圖寫入檔案,並且傳入預設編碼不支援的 Unicode 字元。
解決上述問題的一種方法是在製作 soup 之前對響應文字/字元進行編碼,以獲得所需的結果,如下所示:
responseTxt = response.text.encode('UTF-8') KeyError: [attr]
這是因為在相關標籤未定義 attr 屬性時訪問了 tag['attr']。最常見的錯誤是:“KeyError: 'href'”和“KeyError: 'class'”。如果您不確定是否定義了 attr,請使用 tag.get('attr')。
for item in soup.fetch('a'): try: if (item['href'].startswith('/') or "tutorialspoint" in item['href']): (...) except KeyError: pass # or some other fallback action
AttributeError
您可能會遇到如下 AttributeError:
AttributeError: 'list' object has no attribute 'find_all'
上述錯誤主要是因為您期望 find_all() 返回單個標籤或字串。但是,soup.find_all 返回一個 python 元素列表。
您需要做的就是遍歷列表並從這些元素中捕獲資料。
為了避免在解析結果時出現上述錯誤,將繞過該結果以確保不會將格式錯誤的程式碼片段插入資料庫:
except(AttributeError, KeyError) as er: pass