ian-campbell
ian-campbell

Reputation: 1665

Why does BeautifulSoup remove all the formatting from my HTML?

I have an HTML file which looks more or less like

<body>
    <div>
        <aside class="bg">
            <a href="index.html">Home</a>
        </aside>
    </div>
</body>

But after I parse it with BeautifulSoup and then write it to file, all my formatting is gone. My code looks like:

with open('contact.html', 'r') as f:
    soup = BeautifulSoup(f, "html.parser")
elem = soup.find("aside")
new_html = "<a href="support.html">Support</a>"
new_soup = BeautifulSoup(new_html, "html.parser")
elem.insert(1, newsoup)
with open('contact.html', 'w') as f:
    f.write(str(soup))

The resulting html file looks like

<body>
<div>
<aside class="bg">
<a href="support.html">Support</a>
<a href="index.html">Home</a>
</aside>
</div>
</body>

I don't want to use prettify because I dislike the formatting of it. I just want to keep my formatting the same. Any way I can do that?

Upvotes: 0

Views: 402

Answers (1)

Aditya Katari
Aditya Katari

Reputation: 74

there is a discussion in this thread

Maintaining the indentation of an XML file when parsed with Beautifulsoup

hope this helps

Upvotes: 2

Related Questions