Downloading files off Interet

Blaktyger blaktyger at hotmail.com
Tue Jan 6 17:53:53 CET 2004


Terry Carroll <carroll at tjc.com> wrote in message news:<q02lvv8mr2btomp0e56l64c9cq14qeodcg at 4ax.com>...
> On 5 Jan 2004 23:32:53 -0800, blaktyger at hotmail.com (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.
> >
> >Code:
> >import string
> >import urllib
> >
> >f = urllib.urlopen(""" http://www.somemp3site.com/somemp3.com""")
> >
> >fic=open('mp3file.mp3','w')
> >fic.write(f.read())
> >fic.close()
> >f.close()
> >
> >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:
> 
>  fic=open('mp3file.mp3','wb')
> 
> 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 somemp3site.com 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
> page.
> 
> 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
> http://www.python.org/doc/current/lib/module-urllib.html .
> 
> Instead of:
> 
>  import string
>  import urllib
>  f = urllib.urlopen(""" http://www.somemp3site.com/somemp3.com""")
>  fic=open('mp3file.mp3','w')
>  fic.write(f.read())
>  fic.close()
>  f.close()
> 
> You just do:
> 
>  import urllib
>  urllib.urlretrieve(""" http://www.somemp3site.com/somemp3.com""")
> 
> 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.

Cheating is fun =)
Thanks people!



More information about the Python-list mailing list