跳转到主要内容

将HTML转换为等效的Markdown结构化文本。

项目描述

html2text

CI codecov

html2text 是一个 Python 脚本,它可以将 HTML 页面转换为干净的、易于阅读的纯 ASCII 文本。更好的是,这个 ASCII 文本也恰好是有效的 Markdown(一种文本到HTML的格式)。

用法: html2text [filename [encoding]]

选项 描述
--version 显示程序版本号并退出
-h, --help 显示此帮助信息并退出
--ignore-links 不要包含任何链接的格式化
--escape-all 转义所有特殊字符。输出更难阅读,但避免了角落案例格式化问题。
--reference-links 使用参考链接而不是链接来创建 Markdown
--mark-code 使用 [code]...[/code] 标记预格式化和代码块

有关完整选项列表,请参阅 文档

或者您可以在 Python 中使用它

>>> import html2text
>>>
>>> print(html2text.html2text("<p><strong>Zed's</strong> dead baby, <em>Zed's</em> dead.</p>"))
**Zed's** dead baby, _Zed's_ dead.

或者使用一些配置选项

>>> import html2text
>>>
>>> h = html2text.HTML2Text()
>>> # Ignore converting links from HTML
>>> h.ignore_links = True
>>> print h.handle("<p>Hello, <a href='https://www.google.com/earth/'>world</a>!")
Hello, world!

>>> print(h.handle("<p>Hello, <a href='https://www.google.com/earth/'>world</a>!"))

Hello, world!

>>> # Don't Ignore links anymore, I like links
>>> h.ignore_links = False
>>> print(h.handle("<p>Hello, <a href='https://www.google.com/earth/'>world</a>!"))
Hello, [world](https://www.google.com/earth/)!

最初由Aaron Swartz编写。此代码在GPLv3下分发。

如何安装

html2text可在PyPI上找到 https://pypi.ac.cn/project/html2text/

$ pip install html2text

如何运行单元测试

tox

查看覆盖率结果

coverage html

然后在浏览器中打开 ./htmlcov/index.html 文件。

文档

文档位于 此处

项目详情


下载文件

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

源分布

html2text-2024.2.26.tar.gz (56.5 kB 查看散列值)

上传时间

由以下支持