Can I print HTML how it will be viewed on a web page without tags in python?

I want to print html for a document, but I want it to be formatted as it will be viewed on 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>&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()

But it prints html with tags. Any way to do this in python?

+3
source share
2 answers

Assuming you really want to write this as a text file, you can learn lynx as a visualization tool for html for text.

If you just want to open the html file that you wrote, and it will display just like in a web browser, I suggest saving it as tmp.htmlwell and opening it using a web browser.

+3
...
tmp.write(cgi.escape(soup.renderContents()))
0

All Articles