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'}]
廣告