The HTML parser is a structured markup processing tool. It defines a class called HTMLParser, which we can use to parse HTML files. It comes in handy for
HTMLparser.reset() is one of the methods of HTMLparser
. We can use this to reset any instance of HTMLparser
class. This method clears the buffer of any unprocessed data.
We mostly call
HTMLparser.reset()
at instantiation time.
HTMLParser.reset()
The code below shows how we use HTML parser to separate start tags, end tags, comments, and data from the HTML string. We can see how using the code differs with and without the use of HTMLparser.reset()
.
First, we can see how the code performs without the use of HTMLparser.reset()
.
from html.parser import HTMLParserclass Parser(HTMLParser):# method to append the start tag to the list start_tags.def handle_starttag(self, tag, attrs):global start_tagsstart_tags.append(tag)# method to append the end tag to the list end_tags.def handle_endtag(self, tag):global end_tagsend_tags.append(tag)# method to append the data between the tags to the list all_data.def handle_data(self, data):global all_dataall_data.append(data)# method to append the comment to the list comments.def handle_comment(self, data):global commentscomments.append(data)start_tags = []end_tags = []all_data = []comments = []# Creating an instance of our class.parser = Parser()# Poviding the input.parser.feed('<html><title>Desserts</title><body><p>''I am a fan of frozen yoghurt.</p><')# We can see the input is incomplete. This puts all the# incomplete data in the buffer and waits for next input.print("start tags:", start_tags)print("end tags:", end_tags)print("data:", all_data)print("comments", comments)# Now we feed more data. This is joined by the old data# and treated as one domain.parser.feed('/body><!--My first webpage--></html>')print("")print("After next input:")print("start tags:", start_tags)print("end tags:", end_tags)print("data:", all_data)print("comments", comments)
Now, let’s use HTMLParser.reset()
.
Since buffer is reset, the new data input using
parser.feed
will be treated as a separate new input.
from html.parser import HTMLParserclass Parser(HTMLParser):# method to append the start tag to the list start_tags.def handle_starttag(self, tag, attrs):global start_tagsstart_tags.append(tag)# method to append the end tag to the list end_tags.def handle_endtag(self, tag):global end_tagsend_tags.append(tag)# method to append the data between the tags to the list all_data.def handle_data(self, data):global all_dataall_data.append(data)# method to append the comment to the list comments.def handle_comment(self, data):global commentscomments.append(data)start_tags = []end_tags = []all_data = []comments = []# Creating an instance of our class.parser = Parser()# Poviding the input.parser.feed('<html><title>Desserts</title><body><p>''I am a fan of frozen yoghurt.</p><')# We can see the input is incomplete. This puts all the# incomplete data in the buffer and waits for next input.print("start tags:", start_tags)print("end tags:", end_tags)print("data:", all_data)print("comments", comments)## Now we make use of reset. This will reset any unprecessed data# and clear buffer. Now next input is treated independent# of the last input feed.parser.reset()print("")print("After use of reset:")# feed more inputparser.feed('/body><!--My first webpage--></html>')print("start tags:", start_tags)print("end tags:", end_tags)print("data:", all_data)print("comments", comments)