Newbie: How to download a JPG file from the internet....?

Ignacio Vazquez-Abrams ignacio at openservices.net
Mon Sep 17 16:49:29 EDT 2001


On Mon, 17 Sep 2001, John Branthoover wrote:

> Hello All,
>     Can anyone tell me how to download an image from a web site?
> An example:
> <IMG SRC="./images/getme.jpg"> that is located at www.somewhere.org
>
>     I have already found the urllib module and have used the urlopen
> function to create a string from a web page,  but I don't know how to
> proceed from here.  I can isolate the string -  <IMG
> SRC="./images/getme.jpg">, but then what?  I would like to eventually write
> it to a file on my local drive.  Any information will be greatly
> appreciated.  Thank you for your time.

The first thing you have to do is get the value of the src attribute from the
img tag.

Once you have that, you have to determine if it's an absolute or relative URL.
If it's absolute, then you just get it and write it to a file. If it's
relative, then you have to parse the document for a base tag and apply
the relative URL to that to get an absolute URL.

If there is no base tag, then you have to apply the relative URL to the
absolute URL of the page you are downloading and go from there.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>






More information about the Python-list mailing list