How to save web page's source

Issue

Hello i want save web page’s source codes to any any text file and saving it C:\ folder.

I tried get web page’s source with this code:

html = driver.page_source
print(html)

How to save page source to C:\ folder in a text file?

Solution

Try this:

import urllib.request
site = urllib.request.urlopen('http://somesite.com')
data = site.read()
file = open("C:\\file.txt","wb") #open file in binary mode
file.writelines(data)
file.close()

or this

import urllib.request
def extractHTML(url):
    urllib.request.urlretrieve(url, 'C:\\file.txt')

See more details here

Answered By – Prophet

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published