python下载百度音乐

 1 # -*- coding: UTF-8 -*-
 2 
 3 # Created on 2012-11-21
 4 
 5 
 6 
 7 #下载百度音乐
 8 import urllib
 9 import re
10 
11 baseurl = "http://music.baidu.com"
12 url = "http://music.baidu.com/search/tag?key=经典流行"
13 html = urllib.urlopen(url).read()
14 uri = re.findall(r'/song/\d+', html, re.M)
15 #print uri
16 lst = []
17 for i in uri:
18     link = baseurl+i+"/download"
19    # print link
20     lst.insert(0, link)  #构造lst列表数据
21 print lst
22 for k in lst:
23     res = urllib.urlopen(k).read()
24     #print res
25     down = re.search('http://[^ ]*xcode.[a-z0-9]*' , res, re.M).group()
26     print 'down' 
27     print down
28     #res2 = r"c:\1.html"
29     #s1 = re.findall('title=".*',res, re.M)
30     s1 = re.search('title=".*',res, re.M).group()
31     print s1
32     
33     s2 = re.search('>.*<.a', s1, re.M).group()
34     print s2
35     s3 = s2[1:-3]
36     print s3
37     s3=unicode(s3,"utf-8","ignore").encode("gb2312","ignore")#解决乱码
38     urllib.urlretrieve(down, s3+".mp3")

 

posted on 2012-12-24 14:23  般若2013  阅读(115)  评论(0编辑  收藏  举报

导航