63

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.

xxx
  • 1,153
  • 1
  • 11
  • 23
Kim Hyesung
  • 727
  • 1
  • 6
  • 13

3 Answers3

113

Just convert the soup instance to string and write:

with open("output1.html", "w") as file:
    file.write(str(soup))
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
40

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))
andytham
  • 491
  • 4
  • 10
10

Use unicode to be safe:

with open("output1.html", "w") as file:
    file.write(unicode(soup))
spedy
  • 2,200
  • 25
  • 26
  • 3
    For the benefit of future readers, as mentioned by @andytham, you can only use `unicode()` for Python 2; use `str()` instead for Python 3 – user5305519 Apr 14 '21 at 05:35