将 html 转换为纯文本 Python

Convert html to plain text Python

早上好,我正在寻找一种将html代码转换为纯文本的方法,我留下一个例子

HTML

<div class="card-headline"><h3 class="card-title">

Texto Plano

&lt;div class=&quot;card-headline&quot;&gt;&lt;h3 class=&quot;card-title&quot;&gt;

BeautifulSoup 是一个抓取库,因此它可能不是进行 HTML 渲染的最佳选择。如果不是必须使用BeautifulSoup,你应该看看html2text。例如:

  • 导入html2文本
  • html = open("foobar.html").read()
  • 打印html2text.html2text(html)

这输出:

一些文字更多文字更多文字

  • 列表项
  • 又一个列表项

一些其他文字

  • 列表项
  • 又一个列表项