Downloading files off Interet

B

Blaktyger

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?
Thanks
 
E

Egor Bolonev

Hello, Blaktyger!
You wrote on 5 Jan 2004 23:32:53 -0800:

B> I would like to download some mp3 files from a web site. There is to
B> much of them and I had the idea of writing a script to do it for me.

B> Code:
B> import string
B> import urllib

B> f = urllib.urlopen(""" http://www.somemp3site.com/somemp3.com""")

B> fic=open('mp3file.mp3','w')
B> fic.write(f.read())
B> fic.close()
B> f.close()

B> I ran the program and the file is not playing.
B> Can someone help me?
B> Thanks

may be devas esti wb
fic=open('mp3file.mp3','wb')

With best regards, Egor Bolonev. E-mail: (e-mail address removed)
 
M

Michael Geary

Blaktyger said:
B> I would like to download some mp3 files from a web site.
B> There is to much of them and I had the idea of writing a
B> script to do it for me.

B> Code:
B> import string
B> import urllib

B> f = urllib.urlopen(""" http://www.somemp3site.com/somemp3.com""")

B> fic=open('mp3file.mp3','w')
B> fic.write(f.read())
B> fic.close()
B> f.close()

B> I ran the program and the file is not playing.
B> Can someone help me?
B> Thanks

Egor said:
may be devas esti wb
fic=open('mp3file.mp3','wb')

Or, as someone pointed out when I posted similar code a while back, you can
do the whole thing with a single call:

import urllib
urllib.urlretrieve( 'http://www.foo.com/bar.mp3', 'bar.mp3' )

See the documentation for urllib.urlretrieve for additional information,
especially the optional reporthook argument that lets you provide a callback
function so you can display the progress for a long file.

-Mike
 
T

Terry Carroll

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.
 
B

Blaktyger

Terry Carroll said:
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!
 

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
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top