How to write the output to html file with Python BeautifulSoup

Question:

I modified an html file by removing some of the tags using beautifulsoup. Now I want to write the results back in a html file.
My code:

from bs4 import BeautifulSoup
from bs4 import Comment

soup = BeautifulSoup(open('1.html'),"html.parser")

[x.extract() for x in soup.find_all('script')]
[x.extract() for x in soup.find_all('style')]
[x.extract() for x in soup.find_all('meta')]
[x.extract() for x in soup.find_all('noscript')]
[x.extract() for x in soup.find_all(text=lambda text:isinstance(text, Comment))]
html =soup.contents
for i in html:
    print i

html = soup.prettify("utf-8")
with open("output1.html", "wb") as file:
    file.write(html)

Since I used soup.prettify, it generates html like this:

<p>
    <strong>
     BATAM.TRIBUNNEWS.COM, BINTAN
    </strong>
    - Tradisi pedang pora mewarnai serah terima jabatan pejabat di
    <a href="http://batam.tribunnews.com/tag/polres/" title="Polres">
     Polres
    </a>
    <a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan">
     Bintan
    </a>
    , Senin (3/10/2016).
</p>

I want to get the result like print i does:

<p><strong>BATAM.TRIBUNNEWS.COM, BINTAN</strong> - Tradisi pedang pora mewarnai serah terima jabatan pejabat di <a href="http://batam.tribunnews.com/tag/polres/" title="Polres">Polres</a> <a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan">Bintan</a>, Senin (3/10/2016).</p>
<p>Empat perwira baru Senin itu diminta cepat bekerja. Tumpukan pekerjaan rumah sudah menanti di meja masing masing.</p>

How can I get a result the same as print i (ie. so the tag and its content appear on the same line)? Thanks.

Asked By: Kim Hyesung

||

Answers:

Just convert the soup instance to string and write:

with open("output1.html", "w") as file:
    file.write(str(soup))
Answered By: alecxe

Use unicode to be safe:

with open("output1.html", "w") as file:
    file.write(unicode(soup))
Answered By: spedy

For Python 3, unicode was renamed to str, but I did have to pass in the encoding argument to opening the file to avoid an UnicodeEncodeError.

with open("output1.html", "w", encoding='utf-8') as file:
    file.write(str(soup))
Answered By: andytham
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.