我正在尋找一種方法在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