Python html.parser: a simple HTML and XHTML parser
Answer: The user should subclass HTMLParser and override its methods to implement the desired behavior.
How the parser is used
This module defines a class HTMLParser that can handle HTML and XHTML.
An HTMLParser instance is fed HTML data and calls handler methods when markup is recognized.
Create a parser instance able to parse invalid markup.
This parser does not check that end tags match start tags.
Character references
The default value for argument convert_charrefs is now True.
If convert_charrefs is true, all character references are converted, except the ones in special cases listed in the docs.
handle_data() might be called more than once if convert_charrefs is false and you feed incomplete chunks.
Override handle_starttag and the other handler methods in your subclass.
See also
Claims
- id: C1 claim: The user should subclass HTMLParser and override its methods to implement the desired behavior. source: S1 evidence: "The user should subclass HTMLParser and override its methods to implement the desired behavior."
- id: C2 claim: The parser does not check that end tags match start tags. source: S1 evidence: "This parser does not check that end tags match start tags"
- id: C3 claim: The default value for argument convert_charrefs is now True. source: S1 evidence: "The default value for argument convert_charrefs is now True"
- id: C4 claim: The constructor can parse invalid markup. source: S2 evidence: "Create a parser instance able to parse invalid markup."
Sources
- [S1] html.parser — Simple HTML and XHTML parser. Python Software Foundation. https://docs.python.org/3/library/html.parser.html . License: PSF License Version 2. Accessed 2026-11-24.
- [S2] html.parser documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/html.parser.rst . License: PSF License Version 2. Accessed 2026-11-24.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, html.parser — Simple HTML and XHTML parser; CPython, html.parser documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, html.parser — Simple HTML and XHTML parser; CPython, html.parser documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python html.parser: a simple HTML and XHTML parser." iinventedlight Library, iil:note:f53zh36p, version of 2026-11-24. https://iinventedlight.com/library/notes/python-html-parser/. License: CC-BY-4.0.
@misc{iil_note_f53zh36p,
author = {{iinventedlight}},
title = {{Python html.parser: a simple HTML and XHTML parser}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-24},
url = {https://iinventedlight.com/library/notes/python-html-parser/},
note = {Permanent ID iil:note:f53zh36p. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-html-parser.schema.json (schema; CC-BY-4.0)
- python-html-parser.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-html-parser.md.