I have a code here that finds all the image files using regex by looking up its file extension. Now what I want to do is save it to a specified path on my computer and also preserving its original filenames. My current code finds the images because I tested by printing the 'source' but doesn't save it to the specified directory maybe anyone can help me tweak the code.
Thanks in advance.
Here's my code:
import urllib,re,os
_in = raw_input('< Press enter to download images from first page >')
if not os.path.exists('FailImages'): # Directory that I want to save the image to
os.mkdir('FailImages') # If no directory create it
source = urllib.urlopen('http://www.samplewebpage.com/index.html').read()
imgs = re.findall('\w+.jpg',source) # regex finds files with .jpg extension
# This bit that needs tweaking
for img in imgs:
filename = 'src="'+ img.split('/')[0]
if not os.path.exists(filename):
urllib.urlretrieve(img,filename)