On this page
html
Prev: hmac | Up: stdlib_module | Next: http
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — html
Tags: #public #python3 #stdlib
Import
import html
Overview
HTML character entity escaping (html.escape, html.unescape) plus the streaming html.parser.HTMLParser base class. Always call html.escape() on untrusted text interpolated into HTML templates; use html.parser.HTMLParser for lightweight zero-dependency HTML extraction.
Quick Start
import html
safe = html.escape('<script>alert("xss")</script>', quote=True)
assert safe == "<script>alert("xss")</script>"
assert html.unescape("5 < 10 & 'ok'") == "5 < 10 & 'ok'"
Implementation Walkthrough
1. Extracting Links with html.parser.HTMLParser
from html.parser import HTMLParser
class LinkExtractor(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.links: list[str] = []
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
if tag == "a":
for k, v in attrs:
if k == "href" and v:
self.links.append(v)
parser = LinkExtractor()
parser.feed('<p><a href="https://example.org">Home</a></p>')
parser.close()
assert parser.links == ["https://example.org"]
Gotchas & Boundaries
html.escape()is NOT enough for unquoted attributes,<script>, or CSS contexts: Always quote HTML attributes (title="...") and validate URL schemes (http/https) separately.- Call
parser.close()after feeding chunks to flush any buffered trailing data inHTMLParser. - HTMLParser splits text across multiple
handle_data()calls: Buffer chunks in a list and join them inhandle_endtag().
Common Functions
- html.escape() —
escape(s, quote=True)— Replace special characters "&", "<" and ">" to HTML-safe sequences. - html.unescape() —
unescape(s)— Convert all named and numeric character references (e.g. >, >, &x3e;) in the string s to the corresponding unicode characters.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/hmac
- tuts/python3/stdlib_module/html/escape
- tuts/python3/stdlib_module/html/unescape
- tuts/python3/stdlib_module/http
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/popular_application_domains/automation_devops_and_web_scraping
- tuts/python3/tutorials/popular_application_domains/web_development_and_apis
Related notes
- tuts/python3/stdlib_module/hmac Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/html/escape Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/html/unescape Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/http Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64 Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/email Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; shared tags: #stdlib