python抓取百度音乐盒榜单的音乐,python抓取,[Python]代码#-
python抓取百度音乐盒榜单的音乐,python抓取,[Python]代码#-
[Python]代码
#-*- coding: UTF-8 -*- '''Created on 2012-3-8@author: tiantian'''import urllibimport retop500 = 'http://list.mp3.baidu.com/top/top500.html'songs = []def main(): divr = '<div class="rank-top-wrap clearfix".*?<ul.*?</ul>.*?<ul.*?</ul>.*?</div>' mf = urllib.urlopen(top500) content = mf.read() content = content.decode('gbk') content = re.sub('\n+',' ',content) alldiv = re.findall(divr,content) i =0 for div in alldiv: ulr = '<ul.*?</ul>' allul = re.findall(ulr,div) for ul in allul: lir = '<li.*?</li>' allli = re.findall(lir,ul) for li in allli: if i<245: i = i+1 continue i = i+1 songName = '<div class="music-name">.*?<a.*?>(.*?)</a>.*?</div>' name = re.findall(songName,li) songAuthor = '<div class="singer">.*?<a.*?>(.*?)</a>.*?</div>' author = re.findall(songAuthor,li) songs.append([name[0],author[0]]) songUrl = getSongUrl(name[0],author[0]) try: urllib.urlretrieve(songUrl,'songs/'+name[0]+'-'+author[0]+'.mp3') # 异常检查并不能判断是否下载成功,需要进行其他判断 print i,name[0],author[0],'下载成功' except Exception : print i,name[0],author[0],'没下载成功'def getSongUrl(songName,authorName): '''这里由于歌曲名称和作者名称的不完整,可能导致无法得到url,''' songUrl = 'http://box.zhangmen.baidu.com/x?op=12&count=1&mtype=1&title=%s$$%s$$$$&url=&listenreelect=0&.r=0.1696378872729838' % (urllib.quote(songName.encode('gbk')),urllib.quote(authorName.encode('gbk'))) f = urllib.urlopen(songUrl) c = f.read() url1 = re.findall('<encode>.*?CDATA\[(.*?)\]].*?</encode>',c) url2 = re.findall('<decode>.*?CDATA\[(.*?)\]].*?</decode>',c) if len(url1) <1: return 'http://box.zhangmen.baidu.com/unknow.mp3' try: return url1[0][:url1[0].rindex('/')+1] + url2[0] except Exception: return url1[0]if __name__ == '__main__': main()
相关内容
- python登录猫扑打卡,python猫扑打卡,[Python]代码#
- RhinoScript,,付表皮# -*- cod
- python Web 框架bottle超清晰使用范例,pythonbottle,#coding:
- 一个Python的交互式解释器,python解释器,因为在安卓上装
- 用python正则表达式提取网页的url,python正则表达式,im
- 获取上一个月最后一天的日期,获取最后一天日期,[P
- 获取上一个月第一天的日期,获取第一天日期,[Python]代
- 用python3抓取中文网页,python3中文网页,用python3抓取中文
- 嵌套的dict按对象方式操作,嵌套dict对象,[Python]代码c
- python科学计算,,python科学计算fr
评论关闭