我可以像在没有 python 标签的网页中查看一样打印 HTML 吗?
我想将 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 中做到这一点吗?
I want to print html to a document but I want it formatted as it would be viewed in a web page.
I have the following code:
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()
But this prints out the html with tags. Any way to do this in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您确实想要将其写入文本文件,您可以考虑使用 lynx 作为 html 到文本的渲染器。
如果您只是希望能够打开您编写的 html 文件并使其显示与在 Web 浏览器中完全相同,我建议将其另存为 tmp.html 并使用 Web 浏览器打开它。
Assuming you really do want to write this as a text file, you could look into using lynx as a renderer for html to text.
If you just want to be able to open the html file you wrote and have it appear exactly as it would in a web browser, I suggest saving it as
tmp.html
and opening it with a web browser.