這次這個(gè)真的是干貨哦,昨晚弄了半晚上,,,,從8點(diǎn)吃完飯就開始寫,一直到了快12點(diǎn)才弄好,,,新手,傷不起呀。。。。
先簡(jiǎn)單的說下吧,百度提供了一個(gè)音樂搜索的api,你想百度請(qǐng)求類似于
http://box.zhangmen.baidu.com/x?op=12&count=1&title=最佳損友$$陳奕迅$$$$
的地址,百度會(huì)給你返回一段xml,如下所示
This XML file does not appear to have any style information associated with it. The document tree is shown below.1 8 2829 1 8 2829 1 f98b6772aa97966550ec80617879becee0233bf4 mp3 3778335 128
簡(jiǎn)單的說明下,由于我們要做的只是獲取到歌曲的lrc歌詞地址,所以有用的只有2829這個(gè)標(biāo)簽。
而encode和decode里面的拼接起來就是mp3的下載地址,如本例的
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537
就是下載地址,不過音質(zhì)太差,有時(shí)間在研究下這個(gè)。
繼續(xù)說歌詞,注意lrcid標(biāo)簽里面的2829
http://box.zhangmen.baidu.com/bdlrc/ 這個(gè)是百度lrc歌詞存放地址,
然后本例的歌詞地址是http://box.zhangmen.baidu.com/bdlrc/28/2829.lrc
看到了吧,歌詞地址后面的兩個(gè)數(shù)字的計(jì)算方法是在lrcid除以100所獲得的整數(shù),就是第一個(gè)數(shù)字,然后第二個(gè)數(shù)字就是lrcid,然后后面加上后綴.lrc就搞定了
獲得lrc地址之后就簡(jiǎn)單了,只要請(qǐng)求該地址,然后將獲取到的內(nèi)容寫入文件就ok了。
好了,大概就是這樣,下面是代碼:
import os import os.path import re import eyed3 import urllib2 import urllib from urllib import urlencode import sys import os reload(sys) sys.setdefaultencoding('utf8') music_path = r"E:\music" lrc_path = r"e:\lrc" os.remove('nolrc.txt') os.remove('lrcxml.txt') the_file = open('lrcxml.txt','a') nolrc_file = open('nolrc.txt','a') for root,dirs,files in os.walk(music_path): for filepath in files: the_path = os.path.join(root,filepath) if (the_path.find("mp3") != -1): print the_path the_music = eyed3.load(the_path) the_teg = the_music.tag._getAlbum() the_artist = the_music.tag._getArtist() the_title = the_music.tag._getTitle() # print the_teg # print the_title # print the_artist b = the_title.replace(' ','+') # print b a = the_artist.replace(' ','+') #print urlencode(str(b)) if isinstance(a,unicode): a = a.encode('utf8') song_url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="+b+"$$"+a+"$$$$ " the_file.write(song_url+'\n') page = urllib2.urlopen(song_url).read() print page theid = 0 lrcid = re.compile('(.*?) ',re.S).findall(page) have_lrc = True if lrcid != []: theid = lrcid[0] else: nolrc_file.write(the_title+'\n') have_lrc = False print theid if have_lrc: firstid = int(theid)/100 lrcurl = "http://box.zhangmen.baidu.com/bdlrc/"+str(firstid)+"/"+theid+".lrc" print lrcurl lrc = urllib2.urlopen(lrcurl).read() if(lrc.find('html')== -1): lrcfile = open(lrc_path+"\\"+the_title+".lrc",'w') lrcfile.writelines(lrc) lrcfile.close() else: nolrc_file.write(the_title+'\n') the_file.close() nolrc_file.close() print "end!"
有用第一步請(qǐng)求所獲取到底是xml格式的,所以本來想著解析xml來獲取lrcid,但是在實(shí)現(xiàn)過程中遇到了各種問題,別的還容易,就在這一塊兒浪費(fèi)的時(shí)間最長(zhǎng),糾結(jié)未果之后,只能改用正則表達(dá)式來獲取了。。。只能說明還是學(xué)藝不精呢
原文:逝去日子的博客 ? 使用python掃描本地音樂并下載歌詞
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
