The HTMLParser Module
Module HTMLParser supplies one class, HTMLParser, that you subclass to override methods. HTMLParser.HTMLParser is similar to sgmllib.SGMLParser, but is simpler and able to parse XHTML as well. The main differences between HTMLParser and SGMLParser are the following:
HMTLParserdoes not call methods nameddo_tag,start_tag, andend_tag. To process tags and end tags, your subclassXofHTMLParsermust override methodshandle_starttagand/orhandle_endtagand check explicitly for the tags it wants to process.HMTLParserdoes not keep track of, nor check, tag nesting in any way.HMTLParserdoes nothing, by default, to resolve character and entity references. Your subclassXofHTMLParsermust override methodshandle_charrefand/orhandle_entityrefif it needs to perform processing of such references.
Commonly used methods of an instance h of subclass X of HTMLParser are as follows.
close |
Tells the parser that there is no more input data. When |
feed |
Passes to the parser a part of the text being parsed. The parser processes some prefix of the text and holds the rest in a buffer until the next call to |
handle_charref |
Called to process a character reference |
handle_comment |
Called to handle comments. |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access