В Perl есть для парсинга HTML::TreeBuilder, где можно обратиться к любому объекту в HTML через что-то типа address(".1.2.0.1.0.1.0.0.7.0.0.0"). Есть ли что-то подобное в Python, Ruby, PHP?
http://docs.python.org/lib/module-HTMLParser.html
это парсер а-ля SAX, т.е. код выглядит примерно вот так
(http://docs.python.org/lib/htmlparser-example.html):
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print "Encountered the beginning of a %s tag" % tag
def handle_endtag(self, tag):
print "Encountered the end of a %s tag" % tag
дерева он сам по себе не строит, но по идее сорганизовать
это не сложно (если тэги в html-е правильно закрываются).
возможно для твоей задачи подойдёт BeautifulSoup.
правда таких уродских индексов как у TreeBuilder там нет,
но пользоваться можно.
>>> from BeautifulSoup import *
>>> from urllib import urlopen
>>> s = urlopen('http://linux.org.ru/view-all.jsp').read()
>>> t = BeautifulSoup(unicode(s,'koi8-r'))
>>> t.html.head.title
<title>LINUX.ORG.RU - Русская информация об ОС Linux</title>
>>> t.html.body.table.contents[1].td.img
<img src="/black/newhead1.gif" alt="Русская информация об ОС LINUX" width="266" height="114" />
The html-parser package is a variant language implementation of the Python's SGML parser (sgmllib.py), HTML parser (htmllib.py) and Formatter (formatter.py).