不妨把下mp3和下歌词的代码加上,下歌词的代码供参考:http://hi.baidu.com/butterinsect/item/985b170a18d7e4096c9048b6
python下载歌词
getlrc.py
#-*-coding:utf-8-*-
import sys, urllib, re
reload(sys)
sys.setdefaultencoding('utf-8')
class lrclib:
def __init__(self, music, singer=None):
self.music = music
self.singer = singer
self.fp = open(music+".lrc", "w+")
def getlrc(self):
dir = "http://mp3.sogou.com/lyric.so?query="
music_gbk = urllib.quote(self.music.decode(sys.stdin.encoding).encode('gbk'))
url = dir+music_gbk
if self.singer:
singer_gbk = urllib.quote(self.singer.decode(sys.stdin.encoding).encode('gbk'))
url = url+"+"+singer_gbk
try:
conn = urllib.urlopen(url)
except:
print u'网络不给力, 无法连接'
lines = conn.read()
#records = re.findall("<li>.+LRC歌词下载</a></li>", lines)
try:
records = re.findall('<li>.*LRC\xb8\xe8\xb4\xca\xcf\xc2\xd4\xd8</a></li>', lines)
except:
print '无法找到歌词的链接地址,汗...'
result = records[0]
result = result.split()[1]
left = result[result.find('"')+1:]
left = left[:left.find(':')]
lrcurl = "http://mp3.sogou.com/"
lrcurl = lrcurl+left
try:
lrcconn = urllib.urlopen(lrcurl)
except:
print '网络不给力,无法连接'
print 'lrc文件正在下载中......'
lrc = lrcconn.read()
self.fp.write(lrc)
conn.close()
self.fp.close()
print '下载完成,保存为:%s' %(music+".lrc")
if __name__ == "__main__":
'''getlrc 歌曲名, 歌手名(可选)'''
music = sys.argv[1]
if len(sys.argv)>2:
singer = sys.argv[2]
else:
singer = None
r = lrclib(music, singer)
r.getlrc()
yyt@yyt:~/workspace/pworks$ sudo python getlrc.py 素颜
lrc文件正在下载中......
下载完成,保存为:素颜.lrc |