Install strip-tags
mainInstall the strip-tags CLI tool using pip:
pip install strip-tagsrepository·main·Indexed 18 days ago
https://github.com/simonw/strip-tagsA CLI tool and Python library for stripping HTML tags from content. It features advanced capabilities for targeting specific CSS selectors, minifying whitespace, and preserving specific HTML tags or tag bundles (such as headings, metadata, and tables) to provide structural hints for LLM prompting.
Install the strip-tags CLI tool using pip:
pip install strip-tagsThe strip-tags CLI tool allows you to strip HTML tags from content, optionally targeting specific areas via CSS selectors or preserving certain tags.
Pipe content into the tool:
cat input.html | strip-tags > output.txtOr provide an input file directly:
strip-tags -i input.html > output.txtTo process only specific parts of a page, pass CSS selectors as arguments:
# Process only the .content and .sidebar areas
cat input.html | strip-tags '.content' '.sidebar' > output.txt
# Return only the first element that matches any of the selectors
cat input.html | strip-tags .content --first > output.txtTo remove entire sections (e.g., a <nav> element) identified by a selector, use the -r or --remove flag:
cat input.html | strip-tags -r nav > output.txtTo reduce multiple spaces/tabs to a single space and remove blank lines, use the -m or --minify flag:
cat input.html | strip-tags -m > output.txtWhen preparing content for LLMs, you can preserve specific HTML tags to provide structural hints. Use the -t or --keep-tag option (can be used multiple times).
id= and class= are kept to provide context.href (links), alt (images), and name/value (meta tags) are also kept.--all-attrs to keep all attributes on kept tags.You can pass a bundle name to --keep-tag to preserve groups of related tags:
-t hs: Headings (<h1> through <h6>)-t metadata: <title>, <meta>-t structure: <header>, <nav>, <main>, <article>, <section>, <aside>, <footer>-t tables: <table>, <tr>, <td>, <th>, <thead>, <tbody>, <tfoot>, <caption>, <colgroup>, <col>-t lists: <ul>, <ol>, <li>, <dl>, <dd>, <dt>Keep the <header> section, but specifically ensure <h1> and <li> tags remain:
curl -s https://datasette.io/ | strip-tags header -t h1 -t liYou can import and use the strip_tags function directly in your Python projects.
def strip_tags(
input: str,
selectors: Optional[Iterable[str]]=None,
*,
removes: Optional[Iterable[str]]=None,
minify: bool=False,
remove_blank_lines: bool=False,
first: bool=False,
keep_tags: Optional[Iterable[str]]=None,
all_attrs: bool=False
) -> str:from strip_tags import strip_tags
html = """
<div class='container'>
<h1 class='title'>This has tags</h1>
<p>And whitespace too</p>
</div>
"""
# Strip everything except the <h1> tag within <div> elements, and minify whitespace
stripped = strip_tags(html, selectors=["div"], minify=True, keep_tags=["h1"])
print(stripped)Output:
<h1 class="title">This has tags</h1>
And whitespace toofrom strip_tags import strip_tags
html = """
<div class='container'>
<h1 class='title'>This has tags</h1>
<p>And whitespace too</p>
</div>
"""
stripped = strip_tags(html, selectors=["div"], minify=True, keep_tags=["h1"])
print(stripped)The following options are available for the strip-tags command line tool:
-r, --remove TEXT: Remove content in these selectors-i, --input FILENAME: Input file-m, --minify: Minify whitespace-t, --keep-tag TEXT: Keep these <tags>--all-attrs: Include all attributes on kept tags--first: First element matching the selectors--help: Show this message and exit--version: Show the version and exitUsage: strip-tags [OPTIONS] [SELECTORS]...
Strip tags from HTML, optionally from areas identified by CSS selectors
Example usage:
cat input.html | strip-tags > output.txt
To run against just specific areas identified by CSS selectors:
cat input.html | strip-tags .entry .footer > output.txt
Options:
--version Show the version and exit.
-r, --remove TEXT Remove content in these selectors
-i, --input FILENAME Input file
-m, --minify Minify whitespace
-t, --keep-tag TEXT Keep these <tags>
--all-attrs Include all attributes on kept tags
--first First element matching the selectors
--help Show this message and exit.The strip-tags CLI supports the following options for controlling how HTML is processed:
| Option | Flag | Description |
|---|---|---|
selectors | (positional) | CSS selectors used to identify specific areas of the HTML to process. |
--remove | -r | Specify one or more selectors whose content should be removed. |
--input | -i | Path to the input file. Defaults to - (stdin). |
--minify | -m | Minify whitespace in the output. |
--keep-tag | -t | Specify one or more HTML tags to keep (e.g., p, br). |
--all-attrs | (flag) | When used with --keep-tag, includes all attributes on the kept tags. |
--first | (flag) | Only processes the first element matching the provided selectors. |
strip-tags [SELECTORS] [OPTIONS]
Arguments:
[SELECTORS]... CSS selectors to identify areas to strip
Options:
-r, --remove SELECTOR Remove content in these selectors
-i, --input FILE Input file (default: -)
-m, --minify Minify whitespace
-t, --keep-tag TAG Keep these <tags>
--all-attrs Include all attributes on kept tags
--first First element matching the selectors