Skip to main content

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

项目描述

html2文本

构建状态 覆盖状态 下载 版本 车轮? 格式 执照

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

用法:html2text [filename [encoding]]

选项 描述
--version 显示程序的版本号并退出
-h,--help 显示此帮助消息并退出
--ignore-links 不要包含任何链接格式
--escape-all 转义所有特殊字符。输出可读性较差,但避免了极端情况的格式问题。
--reference-links 使用参考链接而不是链接来创建降价
--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/)!

最初由亚伦·斯沃茨撰写。此代码在 GPLv3 下分发。

如何安装

html2text可在 pypi https://pypi.org/project/html2text/

$ pip install html2text

如何运行单元测试

tox

要查看覆盖结果:

coverage html

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

文档

文档在这里

下载文件

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

源分布

html2text-2020.1.16.tar.gz (49.5 kB 查看哈希

已上传 source

内置分布

html2text-2020.1.16-py3-none-any.whl (32.1 kB 查看哈希

已上传 py3