从HTML中移除标签,可选地从由CSS选择器指定的区域移除
项目描述
strip-tags
从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
标签的name
和value
属性也被保留。
您还可以指定一组标签。例如,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 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 841a158bc8f57e3a891d45132e78c1eb8fdd9b978b8a40e68028446118dedad3 |
|
MD5 | 082e61f5591611c4500ae4baf7b3e984 |
|
BLAKE2b-256 | 59221b50f0c98d35c7e958b080aa7947a90bd74b3dc7e72b759034727edc10e3 |
strip-tags-0.5.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2ced3d245bab6cd2ea34948baabbc244e1ee734c89e65705eff0e8ac6fdef46e |
|
MD5 | d393fbd17d696e9bada559c88bb12e88 |
|
BLAKE2b-256 | f5b60bf8b369ca8b07f8b74b867fdbdd2e693452ab715d726a8f7f134aee44d3 |