strip-tags

repository·main·Indexed 18 days ago

https://github.com/simonw/strip-tags

A 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.

Tokens
1.8K
Snippets
6
Records
6
Agent score
13%

What's inside strip-tags

  1. Use the strip-tags CLI

    main

    The strip-tags CLI tool allows you to strip HTML tags from content, optionally targeting specific areas via CSS selectors or preserving certain tags.

    Basic Usage

    Pipe content into the tool:

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

    Or provide an input file directly:

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

    Targeting CSS Selectors

    To 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.txt

    Removing Content

    To 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.txt

    Minifying Whitespace

    To 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.txt
  2. Keep specific HTML tags using strip-tags

    main

    When 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).

    Tag Attribute Behavior

    • Removed by default: Most attributes are stripped.
    • Preserved by default: id= and class= are kept to provide context.
    • Special preservation: href (links), alt (images), and name/value (meta tags) are also kept.
    • All attributes: Use --all-attrs to keep all attributes on kept tags.

    Tag Bundles

    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>

    Example

    Keep the <header> section, but specifically ensure <h1> and <li> tags remain:

    curl -s https://datasette.io/ | strip-tags header -t h1 -t li
  3. Use strip-tags as a Python library

    main

    You can import and use the strip_tags function directly in your Python projects.

    Function Signature

    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:

    Example Usage

    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 too
    from 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)
  4. Reference: strip-tags CLI options

    main

    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 exit
    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.
  5. Reference the strip-tags CLI options and flags

    main

    The strip-tags CLI supports the following options for controlling how HTML is processed:

    OptionFlagDescription
    selectors(positional)CSS selectors used to identify specific areas of the HTML to process.
    --remove-rSpecify one or more selectors whose content should be removed.
    --input-iPath to the input file. Defaults to - (stdin).
    --minify-mMinify whitespace in the output.
    --keep-tag-tSpecify 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