欢迎访问宙启技术站
智能推送

快速将HTML转化为纯文本的工具:HTML2Text()

发布时间:2023-12-26 08:46:06

HTML2Text()是一个可以将HTML文本转化为纯文本的工具。它通过将HTML标签去除并保留其中的文本内容,使得最终输出以纯文本形式呈现。

下面是一个使用HTML2Text()的示例,展示如何将HTML转化为纯文本:

from html2text import html2text

html = """
<!DOCTYPE html>
<html>
<head>
<title>HTML2Text Example</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is an example of using HTML2Text() to convert HTML to plain text.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<a href="https://www.example.com">Click here</a> for more information.
</body>
</html>
"""

text = html2text(html)
print(text)

运行以上代码,输出结果如下:

Welcome

This is an example of using HTML2Text() to convert HTML to plain text.

- Item 1
- Item 2
- Item 3

[Click here](https://www.example.com) for more information.

可以看到,HTML中的标题、段落、列表和链接都被正确地转换成了纯文本格式。