Saving a picture contained in a web page to disk

Michael Geary Mike at DeleteThis.Geary.com
Wed Nov 12 18:09:50 EST 2003


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






More information about the Python-list mailing list