trying to parse non valid html documents with HTMLParser

Benjamin Niemann pink at
Tue Aug 2 22:14:13 CEST 2005

florent wrote:

> I'm trying to parse html documents from the web, using the HTMLParser
> class of the HTMLParser module (python 2.3), but some web documents are
> not fully valids.

Some?? Most of them :(

> When the parser finds an invalid tag, he raises an 
> exception. Then it seems impossible to resume the parsing just after
> where the exception was raised. I'd like to continue parsing an html
> document even if an invalid tag was found. Is it possible to do this ?

AFAIK not with HTMLParser or htmllib. You might try (if you haven't done
yet) htmllib and see, which parser is more forgiving.

You might pipe the document through an external tool like HTML Tidy
<> before you feed it into

Benjamin Niemann
Email: pink at odahoda dot de

More information about the Python-list mailing list