首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从HTML表中提取数据

从HTML表中提取数据
EN

Stack Overflow用户
提问于 2012-08-03 14:38:49
回答 7查看 85.8K关注 0票数 32

我正在寻找一种在linux shell环境中从HTML中获取某些信息的方法。

这是我感兴趣的一点:

代码语言:javascript
运行
复制
<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中提取的键值对。示例:

代码语言:javascript
运行
复制
Tests         : 103
Failures      : 24
Success Rate  : 76.70 %
and so on..

目前我能做的就是创建一个java程序,它将使用sax解析器或html解析器来提取这些信息。

但是在这里使用java似乎需要在你想要执行的"wrapper“脚本中包含可运行的jar。

我相信一定有"shell“语言可以做同样的事情,比如perl、python、bash等等。

我的问题是我没有这些经验,有人能帮我解决这个“相当容易”的问题吗?

快速更新:

我忘了提一下,我在.html文档中有更多的表和更多的行,很抱歉(一大早)。

更新#2:

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

代码语言:javascript
运行
复制
$ 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)

错误:

代码语言:javascript
运行
复制
$ 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 :

运行Tichodroma的答案会得到这个错误:

代码语言:javascript
运行
复制
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

有什么想法吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11790535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档