gpt4 book ai didi

python - 从 HTML 表中提取数据

转载 作者:IT老高 更新时间:2023-10-28 22:24:46 24 4
gpt4 key购买 nike

我正在寻找一种在 linux shell 环境中从 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>
</table>

我想存储在 shell 变量中,或者在从上面的 html 中提取的键值对中回显这些变量。示例:

Tests         : 103
Failures : 24
Success Rate : 76.70 %
and so on..

目前我能做的是创建一个 java 程序,该程序将使用 sax 解析器或 html 解析器(如 jsoup)来提取此信息。

但是在这里使用 java 似乎是在您要执行的“包装器”脚本中包含可运行 jar 的开销。

我确信肯定有“shell”语言可以做同样的事情,例如 perl、python、bash 等。

我的问题是我对这些的经验为零,有人可以帮我解决这个“相当简单”的问题

快速更新:

我忘了提到我在 .html 文档中有更多表格和更多行,对此我感到抱歉(清晨)。

更新 #2:

尝试像这样安装 Bsoup,因为我没有 root 访问权限:

$ wget http://www.crummy.com/software/BeautifulSoup/bs4/download/4.0/beautifulsoup4-4.1.0.tar.gz
$ tar -zxvf beautifulsoup4-4.1.0.tar.gz
$ cp -r beautifulsoup4-4.1.0/bs4 .
$ vi htmlParse.py # (paste code from ) Tichodromas' answer, just in case this (http://pastebin.com/4Je11Y9q) is what I pasted
$ run file (python htmlParse.py)

错误:

$ python htmlParse.py
Traceback (most recent call last):
File "htmlParse.py", line 1, in ?
from bs4 import BeautifulSoup
File "/home/gdd/setup/py/bs4/__init__.py", line 29
from .builder import builder_registry
^
SyntaxError: invalid syntax

更新 #3:

Running Tichodromas 的回答得到这个错误:

Traceback (most recent call last):
File "test.py", line 27, in ?
headings = [th.get_text() for th in table.find("tr").find_all("th")]
TypeError: 'NoneType' object is not callable

有什么想法吗?

最佳答案

使用 BeautifulSoup4 的 Python 解决方案(Edit: 适当跳过。Edit3: 使用 class="details" 选择 table):

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>
</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')]]

Edit2:要产生所需的输出,请使用以下内容:

for dataset in datasets:
for field in dataset:
print "{0:<16}: {1}".format(field[0], field[1])

结果:

Tests           : 103
Failures : 24
Success Rate : 76.70%
Average Time : 71 ms
Min Time : 0 ms
Max Time : 829 ms

关于python - 从 HTML 表中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11790535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com