This Python tutorial explains html.parser.HTMLParser: Parse HTML Markup. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.
What This Feature Does
Extract selected information from HTML using HTMLParser callbacks.
html.parser.HTMLParser is an event-driven tokenizer for HTML that calls handle_starttag(), handle_endtag(), and handle_data(). It is not a browser DOM engine and does not evaluate scripts or fix every malformed document.
When to Use It
This technique belongs to XML and HTML processing. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.
Step-by-Step Python Example
Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.
from html.parser import HTMLParser
class HeadingCollector(HTMLParser):
def __init__(self):
super().__init__()
self.in_heading = False
self.headings = []
def handle_starttag(self, tag, attrs):
if tag == 'h2':
self.in_heading = True
def handle_endtag(self, tag):
if tag == 'h2':
self.in_heading = False
def handle_data(self, data):
if self.in_heading:
self.headings.append(data)
collector = HeadingCollector()
collector.feed('<h2>News</h2><p>Text</p>')
print(collector.headings)
Understanding the Code
- First, the imports make html.parser available. They provide the filesystem, data or standard-library functions used later.
- Find the call HeadingCollector(). Its parameters determine the specific operation demonstrated in the title.
- Its purpose is to extract selected information from html using htmlparser callbacks. Compare this description against the result printed by the program.
- The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.
Expected Output
['News']
The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.
Important Details and Edge Cases
Collect all hyperlinks from a trusted HTML fragment.
HTMLParser is an event-oriented parser, not a browser DOM implementation or a security sanitizer for arbitrary HTML.
Practice: Try It Yourself
Use a maintained HTML parsing library when a full document tree is needed.
Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.
Official Python Documentation
For the complete reference, see the Python standard library documentation.
Common Beginner Mistakes
- Treating the parser as an HTML sanitizer or assuming all text arrives in one callback.
- Collect all hyperlinks from a trusted HTML fragment.
- HTMLParser is an event-oriented parser, not a browser DOM implementation or a security sanitizer for arbitrary HTML.
Key Takeaways
- Extract selected information from HTML using HTMLParser callbacks.
- html.parser.HTMLParser is an event-driven tokenizer for HTML that calls handle_starttag(), handle_endtag(), and handle_data(). It is not a browser DOM engine and does not evaluate scripts or fix every malformed document.
- Avoid this common pitfall: Treating the parser as an HTML sanitizer or assuming all text arrives in one callback.