Я ищу способ получить определенную информацию из HTML в среде оболочки Linux.
Это немного, что меня интересует:
<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>
И я хочу хранить в переменных оболочки или эхо их в парах значений ключей, извлеченных из выше html. Пример:
Tests : 103
Failures : 24
Success Rate : 76.70 %
and so on..
В настоящее время я могу создать java-программу, которая будет использовать парсер sax или синтаксический анализатор html, такой как jsoup, для извлечения этой информации.
Но использование java здесь кажется накладным с включением runnable jar внутри "wrapper" script, который вы хотите выполнить.
Я уверен, что там должны быть языки "оболочки", которые могут выполнять те же самые i.e 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:
Запуск ответа 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
любые идеи?