I've write this function:
def download_mp3(url,name):
opener1 = urllib2.build_opener()
page1 = opener1.open(url)
mp3 = page1.read()
filename = name+'.mp3'
fout = open(filename, 'wb')
fout.write(mp3)
fout.close()
This function take an url and a name both as string.
Then will download and save an mp3 from the url with the name of the variable name.
the url is in the form http://site/download.php?id=xxxx where xxxx is the id of an mp3
if this id does not exist the site redirects me to another page.
So, the question is: how Can I check if this id exist? I've tried to check if the url exist with a function like this:
def checkUrl(url):
p = urlparse(url)
conn = httplib.HTTPConnection(p.netloc)
conn.request('HEAD', p.path)
resp = conn.getresponse()
return resp.status < 400
But it's seems not working..
Thank you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…