How to write the output to html file with Python BeautifulSoup

Solution 1:

Just convert the soup instance to string and write:

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

Solution 2:

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))