【发布时间】:2020-12-02 10:37:38
【问题描述】:
我想使用 BeautifulSoup 从网站中提取表格并将其存储为结构化数据。 我需要的最终输出是可以导出到带有标题行和多个数据行的 .csv 文件。
我关注了this question 的答案,但似乎 Python(或 BeautifulSoup)的更新需要调整,因为它是 8 年前发布的。我想我已经解决了大部分问题(见下文),但此外,原始答案似乎停止了实际构建数据,而是输出标题数据对列表。
我想使用类似的解决方案,因为它看起来非常接近我的需要。我的数据已经使用 BeautifulSoup 进行了解析,所以我特别要求使用该包而不是 Pandas 的解决方案。
可重现的示例
通过添加第二行来更改原始问题,因为我的数据有很多行。
from bs4 import BeautifulSoup
html = """
<table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
<tr valign="top">
<th>Tests</th>
<th>Failures</th>
<th>Success Rate</th>
<th>Average Time</th>
<th>Min Time</th>
<th>Max Time</th>
</tr>
<tr valign="top" class="Failure">
<td>103</td>
<td>24</td>
<td>76.70%</td>
<td>71 ms</td>
<td>0 ms</td>
<td>829 ms</td>
</tr>
<tr valign="top" class="Failure">
<td>109</td>
<td>35</td>
<td>82.01%</td>
<td>12 ms</td>
<td>2 ms</td>
<td>923 ms</td>
</tr>
</table>"""
soup = BeautifulSoup(html)
table = soup.find("table", attrs={"class":"details"})
# The first tr contains the field names.
headings = [th.get_text() for th in table.find("tr").find_all("th")]
datasets = []
for row in table.find_all("tr")[1:]:
dataset = zip(headings, (td.get_text() for td in row.find_all("td")))
datasets.append(dataset)
print(datasets)
结果应该如下所示(虽然有多行,但我不确定确切的结构)。
[[(u'Tests', u'103'),
(u'Failures', u'24'),
(u'Success Rate', u'76.70%'),
(u'Average Time', u'71 ms'),
(u'Min Time', u'0 ms'),
(u'Max Time', u'829 ms')]]
但是看起来像:
[<zip object at 0x7fb06b5efdc0>, <zip object at 0x7fb06b5ef980>]
尝试的解决方案
我尝试在现有的 for 循环中使用 datasets.append(tuple(dataset)),结果是:
[(('Tests', '103'), ('Failures', '24'), ('Success Rate', '76.70%'), ('Average Time', '71 ms'), ('Min Time', '0 ms'), ('Max Time', '829 ms')),
(('Tests', '109'), ('Failures', '35'), ('Success Rate', '82.01%'), ('Average Time', '12 ms'), ('Min Time', '2 ms'), ('Max Time', '923 ms'))]
这更接近原始答案的预期输出,但显然复制了这些对,而不是创建带有标题和值的数据表。所以我不确定如何处理此时的数据。
【问题讨论】:
-
所以这段代码可以正常工作并正确提取数据。现在你只需要在一行前面加上标题并转换每一行,使得每个元组的最后一个元素以
('Tests', '103')的形式保留,对吧? -
是的,@ForceBru 这听起来像是我想要完成的。
标签: python python-3.x dataframe beautifulsoup html-table