Downloading files off Interet

Terry Carroll carroll at
Tue Jan 6 11:13:26 CET 2004

On 5 Jan 2004 23:32:53 -0800, blaktyger at (Blaktyger) wrote:

>I would like to download some mp3 files from a web site. There is to
>much of them and I had the idea of writing a script to do it for me.
>import string
>import urllib
>f = urllib.urlopen("""""")
>I ran the program and the file is not playing.
>Can someone help me?

A couple possibilities:

First, are you running on Windows?  If so, you want your output file
opened binary:


Second, what does your output file look like?  See if it's a text error
message.  Some sites will check to see whether there is a referring page
from their own web site, and disallow transfer otherwise (or divert to a
different site).  If your site does that, your download
will fail.

The only way I know around this is to give up using urllib, and use
urllib2 instead, opening a Request rather than the url directly, and
specifying the referring page from which you picked up the URL to the MP3

I can give more detail on this, but check the binary open first.

Also, if the referring page is not the issue, you will probably find it
much simpler to use urlretrieve instead.  See .

Instead of:

 import string
 import urllib
 f = urllib.urlopen("""""")

You just do:

 import urllib

That's it.  It's almost cheating.

Unfortunately, there's no urllib2.urlretrieve, so if that referring page
thing is your issue, you can't use urlretrieve.

Hope this helps.

More information about the Python-list mailing list