Saving a picture contained in a web page to disk

G

Giulio Cespuglio

Hi there,

I would like to save to disk a picture contained in a webpage.

Unfortunately the image is dynamic, so the url is actually a query
which looks like

http://adfarm.mediaplex.com/ad/bn/2397-11787-1843-5?mpt=1068644406357385

rather than the standard

http://img-cdn.mediaplex.com/ads/2397/11787/q2_dell_inspiron_120x600.gif

I've been told that a scripting language like Perl, Python or PHP is
my best bet for this kind of tasks, would you agree?
Could you please give a few function names to look at as a starting
point?
That should suffice, although a code snippet would be greatly
appreciated.
;)

Thank you very much in advance for your help.

Regards,
Giulio
 
M

Michael Geary

Giulio Cespuglio:
I would like to save to disk a picture contained in a webpage.

Unfortunately the image is dynamic, so the url is actually a query
which looks like

http://adfarm.mediaplex.com/ad/bn/2397-11787-1843-5?mpt=1068644406357385

rather than the standard

http://img-cdn.mediaplex.com/ads/2397/11787/q2_dell_inspiron_120x600.gif

That doesn't matter at all.
I've been told that a scripting language like Perl, Python or PHP is
my best bet for this kind of tasks, would you agree?

Absolutely! :)
Could you please give a few function names to look at as a starting
point?
That should suffice, although a code snippet would be greatly
appreciated.

Try running this code and see what you think:

#########################

import urllib

def saveUrlToFile( url, fileName ):
f = open( fileName, 'wb' )
data = urllib.urlopen(url).read()
f.write( data )
f.close()

saveUrlToFile(

'http://adfarm.mediaplex.com/ad/bn/2397-11787-1843-5?mpt=1068644406357385',
'test.gif' )

#########################

-Mike
 
I

Irmen de Jong

Giulio said:
I would like to save to disk a picture contained in a webpage.

Unfortunately the image is dynamic, so the url is actually a query
which looks like

http://adfarm.mediaplex.com/ad/bn/2397-11787-1843-5?mpt=1068644406357385

rather than the standard

http://img-cdn.mediaplex.com/ads/2397/11787/q2_dell_inspiron_120x600.gif

Unless you have more requirements that you did not mention, I fail
to see why this is a problem, and why you have to write a program
to do this.

Right click the image, 'save image as'.

Or use a tool such as wget or curl to directly download
the URL above, even the 'dynamic one'.

--Irmen
 
J

John Hunter

Michael> Try running this code and see what you think:

You mean

import urllib
urllib.urlretrieve(url, filename)

right?

<wink>,
JDH
 
M

Michael Geary

John said:
You mean

import urllib
urllib.urlretrieve(url, filename)

right?

<wink>,
JDH

Oh! Thank you--that's much better than the code I posted. :)

-Mike
 
Y

Y2KYZFR1

Irmen de Jong said:
Unless you have more requirements that you did not mention, I fail
to see why this is a problem, and why you have to write a program
to do this.

Right click the image, 'save image as'.

Or use a tool such as wget or curl to directly download
the URL above, even the 'dynamic one'.

--Irmen


you did miss it, he needs to do it with Python as do I anyone have a
helpful piece of code that works as an example?
 
J

John Hunter

If the query is dynamic, you may need to sniff the HTTP headers to
find out what is going on behind the scenes. Mozilla Firebird has a
'Live HTTP Headers' addon module that lets you captue and save the
headers as you browse. You can then sort through them, find out what
is going on, and use the httplib module to code it in python.

Not easy, but doable.

JDH
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top