Problemas con HTMLParser
lorca
pylorca en yahoo.com.ar
Vie Oct 8 01:36:56 CEST 2004
Bueno en algunas paginas cuando trato de parsearlas me tira un error, aparentemente es debido a una malaformacion de la pagina html
por ejemplo trato de parserar los links de una pagina y me tira un error:
>>> import parser
>>> a = parser.LinksParser("http://yahoo.com.ar")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "parser.py", line 18, in __init__
self.feed(x)
File "/usr/lib/python2.3/HTMLParser.py", line 108, in feed
self.goahead(0)
File "/usr/lib/python2.3/HTMLParser.py", line 148, in goahead
k = self.parse_starttag(i)
File "/usr/lib/python2.3/HTMLParser.py", line 239, in parse_starttag
endpos = self.check_for_whole_start_tag(i)
File "/usr/lib/python2.3/HTMLParser.py", line 314, in check_for_whole_start_tag
self.error("malformed start tag")
File "/usr/lib/python2.3/HTMLParser.py", line 115, in error
raise HTMLParseError(message, self.getpos())
HTMLParser.HTMLParseError: malformed start tag, at line 8, column 451
Aca les dejo el codigo, que al al parecer está bien, porque con algunas paginas funciona perfecto y con otras no
import urllib, HTMLParser
#import HTMLParser
class LinksParser(HTMLParser.HTMLParser):
def __init__(self, url):
HTMLParser.HTMLParser.__init__(self)
self.inside_opt=0
self.href = []
self.link_name = []
x = urllib.urlopen(url).read()
self.feed(x)
self.close()
def handle_starttag(self, tag, attributes):
#if tag == "select": self.flag += 1
if tag == "a":
for name, value in attributes:
if name == "href":
self.href.append(value)
self.inside_opt = 1
def handle_endtag(self, tag):
if tag == "a":
self.inside_opt=0
def handle_data(self, data):
if self.inside_opt == 1:
self.link_name.append(data)
def get_options(self):
return self.href, self.link_name
--
l o r c a
Homepage: http://www.pylorca.com.ar
Más información sobre la lista de distribución Python-es