跳转到主要内容

从HTML中移除标签,可选地从由CSS选择器指定的区域移除

项目描述

strip-tags

PyPI Changelog Tests License

从HTML中移除标签,可选地从由CSS选择器指定的区域移除

有关此项目的更多信息,请参阅 llm, ttok and strip-tags—用于处理ChatGPT和其他LLM的CLI工具

安装

使用pip安装此工具

pip install strip-tags

用法

将内容管道输入此工具以移除其中的标签

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

或传递一个文件名

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

针对仅由CSS选择器指定的特定区域运行

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

可以调用多个选择器

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

要返回页面中与任一选择器匹配的第一个元素,请使用--first

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

要移除特定选择器包含的内容 - 例如页面的<nav>部分,请使用-r--remove

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

要压缩空白 - 将多个空格和制表符字符缩减为单个空格,并将多个换行符和空格缩减为最多两个换行符 - 添加-m--minify

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

您也可以使用python -m运行此命令

python -m strip_tags --help

保留指定标签的格式

在将内容传递给语言模型时,有时保留HTML标签的子集很有用 - 例如<h1>This is the heading</h1>,以向模型提供额外的提示。

可以使用-t/--keep-tag选项多次指定应保留的标签。

此示例检查https://datasette.io/<header>部分并保留列表项和<h1>元素周围的标签

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

将从标签中删除所有属性,除了可能为语言模型提供进一步有用提示的id=class=属性之外。

链接的href属性、图像的alt属性以及meta标签的namevalue属性也被保留。

您还可以指定一组标签。例如,strip-tags -t hs 将保留所有级别标题的标签格式。

以下是一些可用的标签组

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

作为 Python 库

您也可以在 Python 代码中使用 strip-tags。函数签名如下

def strip_tags(
    input: str,
    selectors: Optional[Iterable[str]]=None,
    *,
    removes: Optional[Iterable[str]]=None,
    minify: bool=False,
    first: bool=False,
    keep_tags: Optional[Iterable[str]]=None,
    all_attrs: bool=False
) -> str:

以下是一个示例

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)

输出

<h1>This has tags</h1>

And whitespace too

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.

开发

要为此工具做出贡献,首先检出代码。然后创建一个新的虚拟环境

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

现在安装依赖项和测试依赖项

pip install -e '.[test]'

运行测试

pytest

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

strip-tags-0.5.1.tar.gz (11.2 kB 查看哈希值)

上传时间 源代码

构建发行版

strip_tags-0.5.1-py3-none-any.whl (11.2 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面