"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

November 05, 2016

Day #40 - Download Images from Web using Python

This post is about downloading images from a URL
  • Read from the input file
  • Perform recursive download for all files
  • Try catch handled errors and downloaded file successfully


import urllib.request
#Read file tab spaced
f = open("facescrub_actresses.txt",'r')
readlines = f.readlines()
i = 1
title = "female"
for line in readlines:
name = title + str(i) + ".jpg"
i = i+1
#Download only 300 images
if(i==300):
break
values = line.split('\t')
print('Imagepath-',values[3])
try:
#Download Image to directory
urllib.request.urlretrieve(values[3],name)
except:
print("Error for",values[3])

No comments: