Writing to a file in Python is usually a simple 2-liner, which is easy to be written:
1 2 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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() |