downloading web images

J

Joe

I'm just wandering if I'm doing this correct way I'm trying to download an
image and store it into a file this does the job, but created file does not
apear to be an image, it works fine and I can open image, I'm just
wandering if there is a better way of doing this.

htmlSource=urllib.urlopen("http://www.godandscience.org/images/nebula.jpg")
# Read from the object, storing the page's contents in 's'.
s = htmlSource.read()
htmlSource.close()
myfile = open("myfile.jpg", "w")
myfile.write(s)
myfile.close

Thanks
 
D

Do Re Mi chel La Si Do

Hi !

Note :
urllib2.urlopen is more fast and more "modern"
open("myfile.jpg", "wb") if better for binary

Have a good day.

MCI
 
S

Steven D'Aprano

I'm just wandering if I'm doing this correct way I'm trying to download an
image and store it into a file this does the job, but created file does not
apear to be an image, it works fine and I can open image, I'm just
wandering if there is a better way of doing this.

What do you mean that the file does not appear to be an image? If it works
fine and you can open the image, then what is the problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top