python - 我可以打印 HTML,就像在 python 中没有标签的网页中查看的那样吗?

标签 python html printing webpage

我想将 html 打印到文档中,但我希望它的格式与在网页中查看的格式相同。

我有以下代码:

from BeautifulSoup import BeautifulSoup, NavigableString

html = """
<B>THIS IS A TABLE</B>
</div>

<center>
<table width="100%" align="center" cellspacing="0" cellpadding="0" border="0" style="font-size: 10pt; margin-top: 6pt; ">

<tr style="font-size: 7pt;">
    <td colspan="2" align="left" nowrap><B>THIS IS A HEADER1</B></td>
    <td>&nbsp;</td>
    <td colspan="3" align="center" nowrap><B> THIS IS A HEADER2</B></td>
    <td>&nbsp;</td>
    <td colspan="3" align="center" nowrap><B> THIS IS A HEADER3</B></td>
    <td>&nbsp;</td>
    <td colspan="3" align="center" nowrap><B> THIS IS A HEADER4</B></td>
    <td>&nbsp;</td>
</tr>

</table>
"""

soup = BeautifulSoup(''.join(html))

tmp.open('tmp.txt','w')
tmp.write(soup)
tmp.close()

但这会打印出带有标签的 html。有什么办法可以在 python 中执行此操作?

最佳答案

假设你真的确实想把它写成一个文本文件,你可以考虑使用lynx作为 html 到文本的渲染器。

如果您只想打开您编写的 html 文件并使其显示与在网络浏览器中完全一样,我建议将其保存为 tmp.html 并使用网页浏览器。

关于python - 我可以打印 HTML,就像在 python 中没有标签的网页中查看的那样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6071638/

相关文章:

python - 是否可以将 python 对象(特别是 sklearn 模型)存储在内存映射文件中?

html - 背景图片 :url not working

javascript - 打印特定 div 时出错

python - Python中函数打印和打印函数输出之间的差异

python - 直方图错误: 'int' object is not iterable

python - 在 sqlalchemy 中,如何检查 session 是否附加了模型?

javascript - 响应式动态背景图片 - 最佳实践

Python - Selenium - 如何使用浏览器快捷方式

css - 如何设计一个打印友好的网页?

python - 有没有类似 HTTP::Recorder for Python 的东西?