我正在寻找一种在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解析器或jsoup之类的html解析器来提取这些信息。
但是,在这里使用java似乎需要在要执行的“包装器”脚本中包含可运行jar。
我确信一定有“shell”语言可以做同样的事情,例如perl、python、bash等
我的问题是我对这些没有经验,有人能帮我解决这个“相当容易”的问题吗
快速更新:
我忘了说,我在.html文档中有更多的表和行对不起(清晨)。
更新#2:
因为我没有根访问权限,所以试图安装这样的Bsoup:
$ 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
有什么想法吗?
使用BeautifulSoup4(编辑:并适当跳过的Python解决方案。Edit3:使用
class="details"
选择table
):结果如下:
Edit2:若要生成所需的输出,请使用以下内容:
结果:
下面是最重要的答案,适合Python3的兼容性,并通过去除细胞中的空白来改进:
假设html代码存储在mycode.html文件中,下面是一种bash方法:
注意:输出没有完全对齐
相关问题 更多 >
编程相关推荐