472,126 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

Downloading images?

1,247 Expert 1GB
I'm trying to create a script to download a series of images for me, problem is that I have no idea on how to actually get them from the internet to my computer. I've been thinking this:
Expand|Select|Wrap|Line Numbers
  1. import urllib2
  2. request = urllib2.Request(imageurl)
  3. try: response = urllib2.urlopen(request)
  4. except URLError, err:
    # code responsible for going on with the next series
    c_image = response.read()
Would this work and how could I get c_image as a file on my computer?
Oct 16 '08 #1
1 1227
1,247 Expert 1GB
After scrambling together information about this from all over the internet (but mostly the python docs) I found out how to do it.
Expand|Select|Wrap|Line Numbers
  1. import urllib2
  2. request = urllib2.Request(image url)
  3. try: response = urllib2.urlopen(request)
  4. except URLError, err:
  5.     pass #here will come code that is irrelevant to my problem
  6. c_image = response.read()
  7. file = open(desired filename and location, "wb")
  8. file.write(c_image)
  9. file.close()
Still thanks to anyone who was trying to help, but wasn't fast enough.
For anyone trying to learn something from this: I'm using writing mode "wb" to write the file in binary mode, as images aren't ASCII files, and can't be written in the normal writing mode.
Oct 16 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Marc | last post: by
23 posts views Thread by Doug van Vianen | last post: by
3 posts views Thread by xeroxero | last post: by
1 post views Thread by Hans Olav Hygen | last post: by
5 posts views Thread by OBAFGKM_RNS | last post: by
6 posts views Thread by dysfunct | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.