471,573 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Problem using urllib to download images

I am using Python 2.6 on Mac OS 10.3.9.
I have been trying to use:
image = urllib.URLopener()
image.retrieve(url, filename)
to download images from websites. I am able to do so, and end up with
the appropriate file. However, when I try to open the file, I get an
error message. It's something about corrupted data, and an
unrecognised file.
Anyone know what I'm talking about/had similar experiences?
-Taidgh
Nov 3 '08 #1
4 25001
On Nov 3, 11:48*am, tstro...@googlemail.com wrote:
I am using Python 2.6 on Mac OS 10.3.9.
I have been trying to use:
image = urllib.URLopener()
image.retrieve(url, filename)
to download images from websites. I am able to do so, and end up with
the appropriate file. However, when I try to open the file, I get an
error message. It's something about corrupted data, and an
unrecognised file.
Anyone know what I'm talking about/had similar experiences?
-Taidgh
Please show an actual program, complete with error messages.

import urllib
image = urllib.URLopener()
image.retrieve("http://www.python.org/images/success/nasa.jpg",
"NASA.jpg")
Works for me.
Nov 3 '08 #2
Then perhaps it's a problem with my os.
[TERMINAL SESSION]
[18:16:33 Mon Nov 03] python
Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>import urllib
url = 'http://www.google.com/webhp?hl=en'
filename = 'logo.gif'
image = urllib.URLopener()
image.retrieve(url, filename)
('logo.gif', <httplib.HTTPMessage instance at 0x5196e8>)
[/TERMINAL SESSION]
And here's the error message I get when I try to open it: "File Error:
Couldn't open the file. It may be corrupt or a file format that
Preview doesn't recognize.".
I have had a similar result trying to open it with other programs.
-Taidgh
Nov 3 '08 #3
On Mon, Nov 3, 2008 at 2:21 PM, <ts******@googlemail.comwrote:
Then perhaps it's a problem with my os.
[TERMINAL SESSION]
[18:16:33 Mon Nov 03] python
Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>import urllib
url = 'http://www.google.com/webhp?hl=en'
That's not the URL of an image file. Maybe you're looking for
url = 'http://www.google.com/intl/en_ALL/images/logo.gif'
>>>filename = 'logo.gif'
image = urllib.URLopener()
image.retrieve(url, filename)
('logo.gif', <httplib.HTTPMessage instance at 0x5196e8>)
[/TERMINAL SESSION]
And here's the error message I get when I try to open it: "File Error:
Couldn't open the file. It may be corrupt or a file format that
Preview doesn't recognize.".
I have had a similar result trying to open it with other programs.
That's because you downloaded some HTML and saved it in a file named
logo.gif. That's unlikely to work in any image viewing program. Try
opening the file you downloaded in a text editor and you'll see.

--
Jerry
Nov 3 '08 #4
That's because you downloaded some HTML and saved it in a file named
logo.gif. That's unlikely to work in any image viewing program. Try
opening the file you downloaded in a text editor and you'll see.

--
Jerry
Aha, so the first param is the file, and second is the name you save
the files as. Thankyou, for pointing out my stupid mistake. I was
confused by trying to replicate a program called 'comicdownloader.py'
off of uselesspython.com. I thought that the first param was the page
containing the file, and the second was the file. And that the file
would simply be saved as it's name on the website. Thanks again.
-Taidgh
Nov 3 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by jeff | last post: by
1 post views Thread by ralobao | last post: by
5 posts views Thread by comshiva | last post: by
28 posts views Thread by Jay | last post: by
6 posts views Thread by justsee | last post: by
1 post views Thread by onceuponapriori | last post: by
5 posts views Thread by althafexcel | last post: by
1 post views Thread by Abandoned | last post: by
2 posts views Thread by ShashiGowda | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | 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.