Python将HTML转换为文本并模仿格式
我正在学习BeautifulSoup,并找到了许多“ html2text”解决方案,但是我正在寻找的解决方案应该模仿格式:
<ul>
<li>One</li>
<li>Two</li>
</ul>
会成为
* One
* Two
和
Some text
<blockquote>
More magnificent text here
</blockquote>
Final text
至
Some text
More magnificent text here
Final text
我正在阅读文档,但看不到任何直接信息。有什么帮助吗?我愿意使用除beautifulsoup之外的其他方法。
-
看一下Aaron
Swartz的html2text脚本(可以与一起安装pip install html2text
)。注意,输出是有效的Markdown。如果由于某种原因无法完全满足您的需要,则可以通过一些微不足道的调整来获得问题的确切输出:In [1]: import html2text In [2]: h1 = """<ul> ...: <li>One</li> ...: <li>Two</li> ...: </ul>""" In [3]: print html2text.html2text(h1) * One * Two In [4]: h2 = """<p>Some text ...: <blockquote> ...: More magnificent text here ...: </blockquote> ...: Final text</p>""" In [5]: print html2text.html2text(h2) Some text > More magnificent text here Final text