
- 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 - 解析表格
除了文字內容外,HTML文件還可以包含以HTML表格形式的結構化資料。使用Beautiful Soup,我們可以將表格資料提取到Python物件(例如列表或字典)中,如果需要,可以將其儲存到資料庫或電子表格中,並進行處理。在本章中,我們將使用Beautiful Soup解析HTML表格。
雖然Beautiful Soup沒有專門用於提取表格資料的函式或方法,但我們可以透過簡單的抓取技術來實現。就像SQL或電子表格中的任何表格一樣,HTML表格由行和列組成。
HTML使用<table>標籤來構建表格結構。其中包含一個或多個巢狀的<tr>標籤,每個標籤代表一行。每一行包含<td>標籤,用於儲存該行每個單元格中的資料。第一行通常用於列標題,標題放置在<th>標籤而不是<td>標籤中。
下面的HTML指令碼在瀏覽器視窗中呈現一個簡單的表格:
<html> <body> <h2>Beautiful Soup - Parse Table</h2> <table border="1"> <tr> <th>Name</th> <th>Age</th> <th>Marks</th> </tr> <tr class='data'> <td>Ravi</td> <td>23</td> <td>67</td> </tr> <tr class='data'> <td>Anil</td> <td>27</td> <td>84</td> </tr> </table> </body> </html>
請注意,資料行的外觀使用CSS類data進行自定義,以便將其與標題行區分開來。
我們現在將學習如何解析表格資料。首先,我們在BeautifulSoup物件中獲取文件樹。然後將所有列標題收集到一個列表中。
示例
from bs4 import BeautifulSoup soup = BeautifulSoup(markup, "html.parser") tbltag = soup.find('table') headers = [] headings = tbltag.find_all('th') for h in headings: headers.append(h.string)
然後獲取標題行後面的具有class='data'屬性的資料行標籤。建立一個字典物件,其中列標題作為鍵,每個單元格中的對應值作為值,並將該字典物件新增到字典物件列表中。
rows = tbltag.find_all_next('tr', {'class':'data'}) trows=[] for i in rows: row = {} data = i.find_all('td') n=0 for j in data: row[headers[n]] = j.string n+=1 trows.append(row)
在trows中收集字典物件的列表。然後,您可以將其用於不同的目的,例如儲存到SQL表中,儲存為JSON或pandas DataFrame物件。
完整的程式碼如下:
markup = """ <html> <body> <p>Beautiful Soup - Parse Table</p> <table> <tr> <th>Name</th> <th>Age</th> <th>Marks</th> </tr> <tr class='data'> <td>Ravi</td> <td>23</td> <td>67</td> </tr> <tr class='data'> <td>Anil</td> <td>27</td> <td>84</td> </tr> </table> </body> </html> """ from bs4 import BeautifulSoup soup = BeautifulSoup(markup, "html.parser") tbltag = soup.find('table') headers = [] headings = tbltag.find_all('th') for h in headings: headers.append(h.string) print (headers) rows = tbltag.find_all_next('tr', {'class':'data'}) trows=[] for i in rows: row = {} data = i.find_all('td') n=0 for j in data: row[headers[n]] = j.string n+=1 trows.append(row) print (trows)
輸出
[{'Name': 'Ravi', 'Age': '23', 'Marks': '67'}, {'Name': 'Anil', 'Age': '27', 'Marks': '84'}]
廣告