Writing to a text file in Python from a variable, ignoring encoding problems
Writing to a file in Python is usually a simple 2-liner, which is easy to be written:
with io.open("soup.txt", "w") as file:
file.write(soup_written)
However, problems with the encoding may cause some research needed. Concerning the encoding this is a great article from the creator of StackOverflow.com. Anyway, if the encoding is passed correctly then nothing can stop us from reading the article and writing its HTML to a file, if this is our ultimate goal:
import urllib.request
from bs4 import BeautifulSoup
import io
def main():
resp = urllib.request.urlopen("https://www.vitoshacademy.com")
soup = BeautifulSoup(resp,from_encoding=resp.info().get_param('charset'))
soup_written = soup.prettify()
print (soup_written)
with io.open("soup.txt", "w", encoding="utf-8") as file:
file.write(soup_written)
if __name__== "__main__":
main()