Downloading a .mp3 file in python

Viewed 8665

I am trying to download a .mp3 file, although, when I download it, it is downloaded but sounds like sqeaks and other strange noises through my speakers (similar to when you have a .mp3 file that has errors in the coding).

Here is my current code:

# sets the song url to mp3link variable.
mp3link = dLink[0]

# opens the .mp3 file - using the same procedure as above.
openmp3 = open('testing.mp3', 'w')
dl = urllib2.urlopen(mp3link)
dl2 = dl.read()

# writes the .mp3 file to the file 'testing.mp3' which is in the variable openmp3.
openmp3.write(dl2)
openmp3.close()

print 'done'

I am aware that I could use this code as a faster method:

dlmp3 = urllib2.urlopen(url)
with open('testing2.mp3', 'wb') as filee:
    filee.write(dlmp3.read())

Is there somebody that can tell me what I am doing wrong and how I would be able to fix it? Thanks.

1 Answers
Related