title: Saving Images from URL
body: <p>I'm trying to create a script that will download and save all image files from a website into a directory. This is my code but I can't get it to download the files and save them, can anyone see why not? I know there's another way it can be done using BeautifulSoup/Soup but I'm trying to get learn about regex and what can be done with it. Can anyone help?</p>
  
  <pre><code>import traceback
  import sys, re
  from time import sleep
  from urllib import urlretrieve
  
  images = re.findall(r'([-\w]+\.(?:jpg))', webpage.read())
  try:
  
      filename='./dogg/file.html'
      urlretrieve('http://dogpicturesite.com/', filename)
      webpage=open(filename, 'r')
      print "Downloading Images....."
      time.sleep(5)
      print "Images Downloaded."
      print images
  
  except:
      print "Failed to Download Images"
      raw_input('Press Enter to exit...')
      sys.exit()
  </code></pre>
  
  <p>From this script I can list the .jpg files on the webpage, what I now want to do is download them but from here I'm unsure how to download them. I thought it would be easier with the above script but would it be easier to edit the one below?</p>
  
  <pre><code>import sys, urllib, re
      def imagefiles(webpage):
          print ' imagefiles()'
          images = re.findall(r'([-\w]+\.(?:jpg))', webpage)
  
          for image in images:
              print image
  
      def main():
          sys.argv.append('http://dogpicturesite.com/')
          if len(sys.argv) != 2:
              print '[-] Image Files'
              return
          page = webpage.webpage(sys.argv[1])
          imagefiles(webpage)
  </code></pre>