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'}]
广告
© . All rights reserved.