Skip to main content
Sanitizes and cleans HTML content by removing unwanted elements, attributes, and whitespace. Provides fine-grained control over each cleaning operation through configurable options.

Examples

Arguments

html
str
required
The HTML content to sanitize
remove_scripts
bool
Remove all <script> elements. Defaults to True.
remove_styles
bool
Remove all <style> elements. Defaults to True.
remove_svgs
bool
Remove all <svg> elements. Defaults to True.
remove_comments
bool
Remove HTML comments. Defaults to True.
remove_long_attributes
bool
Remove attributes longer than max_attribute_length. Defaults to True.
max_attribute_length
int
Maximum length for attributes before removal. Defaults to 500.
preserve_attributes
list[str]
List of attribute names to always preserve. Defaults to [“class”, “src”].
remove_empty_tags
bool
Remove empty tags (except preserved ones). Defaults to True.
preserve_empty_tags
list[str]
List of tag names to preserve even when empty. Defaults to [“img”].
minify_whitespace
bool
Remove extra whitespace between tags and empty lines. Defaults to True.

Returns: str

The sanitized HTML string