Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is HTMLParser.feed in Python?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The HTML parser is a structured markup processing tool. It defines a class called HTMLParser, ​which is used to parse HTML files. It comes in handy for web crawling​Web Crawler is an internet bot which is used to systematically browse and surf WWW (World wide web)..

HTMLParser.feed is one of the methods of HTML parser. We use this to input data to the HTML parser.

The data is in the format of a string and should be complete. Complete data means that all tags are complete and nothing is missing. For example, </p, is incomplete data because the closing > is missing. The parser buffers all incomplete data.

Syntax

HTMLParser.feed(data)

Code

This code below shows how we can use HTML parser to separate start tags, end tags, comments, and data from the HTML string.

from html.parser import HTMLParser
class Parser(HTMLParser):
# method to print the start tags.
def handle_starttag(self, tag, attrs):
print("start tag: ",tag)
# method to print the end tags.
def handle_endtag(self, tag):
print("end tag: ",tag)
# method to print the data between the tags.
def handle_data(self, data):
print("Data: ",data)
# method to print the comments.
def handle_comment(self, data):
print("comment: ",data)
# Creating an instance of our class.
parser = Parser()
# Poviding the input.
# user parser.feed for input
parser.feed('<html><title>Desserts</title><body><p>'
'I am a fan of frozen yoghurt.</p><'
'/body><!--My first webpage--></html>')

RELATED TAGS

python

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring