> ## Documentation Index
> Fetch the complete documentation index at: https://intunedhq.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# sanitize_html

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

```python theme={null}
def sanitize_html(
    html: str,
    *,
    remove_scripts: bool,
    remove_styles: bool,
    remove_svgs: bool,
    remove_comments: bool,
    remove_long_attributes: bool,
    max_attribute_length: int,
    preserve_attributes: list[str] | None,
    remove_empty_tags: bool,
    preserve_empty_tags: list[str] | None,
    minify_whitespace: bool,
) -> str
```

## Examples

```python Basic Sanitization theme={null}
from typing import TypedDict
from playwright.async_api import Page
from intuned_browser import sanitize_html
class Params(TypedDict):
    pass
async def automation(page: Page, params: Params, **_kwargs):
    await page.goto("https://books.toscrape.com")
    first_row = page.locator("ol.row").locator("li").first
    # Get the HTML of the first row.
    html = await first_row.inner_html()
    # Sanitize the HTML.
    sanitized_html = sanitize_html(html)
    # Log the sanitized HTML.
    print(sanitized_html)
    # Return the sanitized HTML.
    return sanitized_html
```

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


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.