0

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?

ian-campbell
  • 1,605
  • 1
  • 20
  • 42
  • 1
    How should it know how to "keep your formatting the same" if you are editing the parsed HTML as an element tree? In particular, how should it decide how much to indent the inserted element? – Karl Knechtel May 16 '20 at 03:11
  • I guess `soup.prettify()` is my only option and I'll have to accept the formatting it gives. – ian-campbell May 16 '20 at 03:32

1 Answers1

2

there is a discussion in this thread

Maintaining the indentation of an XML file when parsed with Beautifulsoup

hope this helps