Search

Download Webpage with Python and Urllib

The Python Urllib Module is present by Default on your System so you can make a little Python Script you call to Read and Save a Local Copy of a Webpage.

Open for Edit a File named for example dwnlWebP2Local.py and insert this code

#!/usr/bin/python
import urllib

# Ask for Target Url Input
url = raw_input("Enter the Target URL : ")
# Ask for File Name Input
fileName = raw_input("Enter the File Name: ")

# Add http:// to your Url Name
url = 'http://' + url
# Open Connection to the Target Url
sock=urllib.urlopen(url)
# Open the Local File for Writing
myFile = open(fileName,'w')
# Store the Html Content in a File
for lines in sock.readlines():
	myFile.write(lines)
# Close the Local File
myFile.close()
# Close the Urllib Connection to your Url
sock.close()

Now on a Unix like System you should make your File Executable by:

sudo chmod +x dwnlWebP2Local.py

Next when you need to Download a Webpage you can Call your Python Script with:

python /path/To/dwnlWebPLocal.py

You Give Url and Filename to Save a Webpage Copy on your System.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer