Skip to main content

Strip tags from HTML, optionally from areas identified by CSS selectors

Project description

strip-tags

PyPI Changelog Tests License

Strip tags from HTML, optionally from areas identified by CSS selectors

See llm, ttok and strip-tags—CLI tools for working with ChatGPT and other LLMs for more on this project.

Installation

Install this tool using pip:

pip install strip-tags

Usage

Pipe content into this tool to strip tags from it:

cat input.html | strip-tags > output.txt

Or pass a filename:

strip-tags -i input.html > output.txt

To run against just specific areas identified by CSS selectors:

strip-tags '.content' -i input.html > output.txt

This can be called with multiple selectors:

cat input.html | strip-tags '.content' '.sidebar' > output.txt

To return just the first element on the page that matches one of the selectors, use --first:

cat input.html | strip-tags .content --first > output.txt

To remove content contained by specific selectors - e.g. the <nav> section of a page, use -r or --remove:

cat input.html | strip-tags -r nav > output.txt

To minify whitespace - reducing multiple space and tab characters to a single space, removing any remaining blank lines - add -m or --minify:

cat input.html | strip-tags -m > output.txt

You can also run this command using python -m like this:

python -m strip_tags --help

Keeping the markup for specified tags

When passing content to a language model, it can sometimes be useful to leave in a subset of HTML tags - <h1>This is the heading</h1> for example - to provide extra hints to the model.

The -t/--keep-tag option can be passed multiple times to specify tags that should be kept.

This example looks at the <header> section of https://datasette.io/ and keeps the tags around the list items and <h1> elements:

curl -s https://datasette.io/ | strip-tags header -t h1 -t li
<li>Uses</li>
<li>Documentation Docs</li>
<li>Tutorials</li>
<li>Examples</li>
<li>Plugins</li>
<li>Tools</li>
<li>News</li>
<h1>
    Datasette
</h1>
Find stories in data

All attributes will be removed from the tags, except for the id= and class= attribute since those may provide further useful hints to the language model.

The href attribute on links, the alt attribute on images and the name and value attributes on meta tags are kept as well.

You can also specify a bundle of tags. For example, strip-tags -t hs will keep the tag markup for all levels of headings.

The following bundles can be used:

  • -t hs: <h1>, <h2>, <h3>, <h4>, <h5>, <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>

As a Python library

You can use strip-tags from Python code too. The function signature looks like this:

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:

Here's an example:

from strip_tags import strip_tags

html = """
<div>
<h1>This has tags</h1>

<p>And whitespace too</p>
</div>
Ignore this bit.
"""
stripped = strip_tags(html, ["div"], minify=True, keep_tags=["h1"])
print(stripped)

Output:

<h1>This has tags</h1>

And whitespace too

Use remove_blank_lines=True to remove any remaining blank lines from the output.

strip-tags --help

Usage: 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.

Development

To contribute to this tool, first checkout the code. Then create a new virtual environment:

cd strip-tags
python -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strip_tags-0.6.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strip_tags-0.6-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file strip_tags-0.6.tar.gz.

File metadata

  • Download URL: strip_tags-0.6.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for strip_tags-0.6.tar.gz
Algorithm Hash digest
SHA256 4a7c637d9d937cf833f6adcf38528b8ec8e0ad1b3fdc5ac8f6d167716e22422d
MD5 2fa9f0b2099057e034250504a603931d
BLAKE2b-256 91d34b3fe3068e96acf1cd71a208668d254d60f3081bfbc88739e0484de107a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for strip_tags-0.6.tar.gz:

Publisher: publish.yml on simonw/strip-tags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file strip_tags-0.6-py3-none-any.whl.

File metadata

  • Download URL: strip_tags-0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for strip_tags-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 207357a29454aa4ff36f036f692358e482ae566e6440e4fcd3f9f8ad16f9489a
MD5 aa85dcdfbde6a445bc7fb758dfb375a5
BLAKE2b-256 4cac322eb271b735b0d38fbe9585070c998b41ddf5822016a4533de1500a6fec

See more details on using hashes here.

Provenance

The following attestation bundles were made for strip_tags-0.6-py3-none-any.whl:

Publisher: publish.yml on simonw/strip-tags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page