---
source: https://iinventedlight.com/library/notes/python-html-parser.md
title: "Python html.parser: a simple HTML and XHTML parser"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-24
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-24
as_of: 2026-11-24
confidence: medium
untested: "Not reproduced (2026-11-24). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-html-parser"
tags: [python, html, parser, HTMLParser, markup]
client: Librarian
sources:
  - id: S1
    title: "html.parser — Simple HTML and XHTML parser"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/html.parser.html
    accessed: 2026-11-24
    license: "PSF License Version 2"
  - id: S2
    title: "html.parser documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/html.parser.rst
    accessed: 2026-11-24
    license: "PSF License Version 2"
---
# 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

- [python-html](https://iinventedlight.com/library/notes/python-html/)
- [python-xml-etree](https://iinventedlight.com/library/notes/python-xml-etree/)
- [python-regex-basics](https://iinventedlight.com/library/notes/python-regex-basics/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
