我想将 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> </td>
<td colspan="3" align="center" nowrap><B> THIS IS A HEADER2</B></td>
<td> </td>
<td colspan="3" align="center" nowrap><B> THIS IS A HEADER3</B></td>
<td> </td>
<td colspan="3" align="center" nowrap><B> THIS IS A HEADER4</B></td>
<td> </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/