I need save the HTML code of any website in a txt file, is a very easy exercise but I have doubts with this because a have a function that do this:
import urllib.request
def get_html(url):
f=open('htmlcode.txt','w')
page=urllib.request.urlopen(url)
pagetext=page.read() ## Save the html and later save in the file
f.write(pagetext)
f.close()
But this doesn't work.
Open the source TXT file in Python. Call the 'save()' method, passing an output filename with HTML extension. Get the result of TXT conversion as HTML.
Use open() and file. write() to write to an HTML file Use file. write(data) to write data to the file . Use file. close() to close the file after writing.
Click and drag to select the text on the Web page you want to extract and press “Ctrl-C” to copy the text. Open a text editor or document program and press “Ctrl-V” to paste the text from the Web page into the text file or document window. Save the text file or document to your computer.
I use Python 3
.pip install requests
- after install requests
library you can save a webpage in txt file.
import requests
url = "https://stackoverflow.com/questions/24297257/save-html-of-some-website-in-a-txt-file-with-python"
r = requests.get(url)
with open('file.txt', 'w') as file:
file.write(r.text)
Easiest way would be to use urlretrieve:
import urllib
urllib.urlretrieve("http://www.example.com/test.html", "test.txt")
For Python 3.x the code is as follows:
import urllib.request
urllib.request.urlretrieve("http://www.example.com/test.html", "test.txt")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With