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

发布于 2024-11-08 16:05:11 字数 1082 浏览 0 评论 0原文

我想将 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 中做到这一点吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-11-15 16:05:11

假设您确实想要将其写入文本文件,您可以考虑使用 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.

无需解释 2024-11-15 16:05:11
...
tmp.write(cgi.escape(soup.renderContents()))
...
tmp.write(cgi.escape(soup.renderContents()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文