我正在寻找一种方法在Linux shell环境中从HTML获取某些信息。从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..
我可以在此刻要做的就是创建使用SAX解析器或HTML解析器如jsoup提取此信息的Java程序。
但是,在这里使用java似乎是在你要执行的“包装器”脚本中包含可运行jar的开销。
我敢肯定,必须有“壳”的语言,有可以做同样的也就是Perl,Python和庆典等
我的问题是,我有这些零经验,能够有人帮助我解决这个“相当简单”的问题
快速更新:
我忘了提,我的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:
运行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
什么想法?
有一个不错的python库可以帮助:BeautifulSoup - > http://www.crummy.com/software/BeautifulSoup/bs4/doc/。 – 2012-08-03 06:53:05
@Jakob S.谢谢你的评论,因为我告诉你我是新手,所以我下载了tarbal并试图安装它'python setup.py install'得到这个权限错误'错误:无法创建'/ usr/lib/python2.4/site-packages/bs4':Permission denied',如何指定安装它的目录。在安装其他命令时是否有类似于“-prefix”的内容 – 2012-08-03 07:06:28
我不得不承认,如果您没有root访问权限,我不知道如何实现这一目标 - 并且此刻我还没有Linux。原则上,应该可以简单地将软件包复制到与源.py文件相关的正确目录中,以便解释程序可以找到它。 – 2012-08-03 07:14:36