473,569 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.URLopene r()
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 25842
On Nov 3, 11:48*am, tstro...@google mail.com wrote:
I am using Python 2.6 on Mac OS 10.3.9.
I have been trying to use:
image = urllib.URLopene r()
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.URLopene r()
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:66 715M, 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.URLopene r()
image.retriev e(url, filename)
('logo.gif', <httplib.HTTPMe ssage 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******@googl email.comwrote:
Then perhaps it's a problem with my os.
[TERMINAL SESSION]
[18:16:33 Mon Nov 03] python
Python 2.6 (trunk:66714:66 715M, 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.URLopene r()
image.retrie ve(url, filename)
('logo.gif', <httplib.HTTPMe ssage 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 'comicdownloade r.py'
off of uselesspython.c om. 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1754
by: jeff | last post by:
Hiya im trying to pull tags off a website using python ive got a few things running that have the potential to work its just i cant get them to becuase of certain errors? basically i dont what to download the images and all the stuff just the html and then work from there, i think its timing out because its trying to downlaod the images...
1
2750
by: ralobao | last post by:
Hi, i am doing a program to download all images from an specified site. it already works with most of the sites, but in some cases like: www.slashdot.org it only download 1kb of the image. This 1kb is a html page with a 503 error. What can i make to really get those images ? Thanks
5
2586
by: comshiva | last post by:
Hi all, I have converted my existing ASP.NET project from 1.1 to 2.0 and i have found that everything works fine except the linkbutton control in my datagrid which throws an javascript error when clicked. I thought the control might be the problem, so i deleted the old control and binded the new linkbutton control but am still getting the...
28
2241
by: Jay | last post by:
OK, I have this XML doc, i dont know much about XML, but what i want to do is take certain parts of the XML doc, such as </title> blah </title> and take just that and put onto a text doc. Then same thing doe the </body> part. Thats about it, i checked out some of the xml modules but dont understand how to use them. Dont get parsing, so if...
6
5811
by: justsee | last post by:
Hi, I'm using Python 2.3 on Windows for the first time, and am doing something wrong in using urllib to retrieve images from urls embedded in a csv file. If I explicitly specify a url and image name it works fine(commented example in the code), but if I pass in variables in this for loop it throws errors: --- The script: import csv,...
1
2573
by: onceuponapriori | last post by:
Greetings gents. I'm a Railser working on a django app that needs to do some scraping to gather its data. I need to programatically access a site that requires a username and password. Once I post to the login.php page, there seems to be a redirect and it seems that the site is using a session (perhaps a cookie) to determine whether the...
5
3681
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually for repeated html im using the external js, i mean the TOP, BOTTOM they are repeated in every page, so i include them as functions in the external...
1
12454
by: Abandoned | last post by:
Hi.. I want to set 30 second urllib.urlretrieve timeout.. Because if urllib.urlretrieve can't connect to page wait 1-2 hour... I download the images to my server with urlretrieve if you know the better way please help me. I'm sorry my bad english..
2
1991
by: ShashiGowda | last post by:
Hey there i made a script to download all images from a web site but it runs damn slow though I have a lot of bandwidth waiting to be used please tell me a way to use urllib to open many connections to the server to download many pics simultaneously.... Any off question suggestions are also ok...
0
7625
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7935
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8144
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7692
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7992
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6313
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3677
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
969
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.