python中使用百度音樂搜索的api下載指定歌曲的lrc歌詞
這次這個真的是干貨哦,昨晚弄了半晚上,,,,從8點吃完飯就開始寫,一直到了快12點才弄好,,,新手,傷不起呀。。。。
先簡單的說下吧,百度提供了一個音樂搜索的api,你想百度請求類似于
http://box.zhangmen.baidu.com/x?op=12&count=1&title=最佳損友$$陳奕迅$$$$
的地址,百度會給你返回一段xml,如下所示
This XML file does not appear to have any style information associated with it. The document tree is shown below. <result> <count>1</count> <url> <encode> <![CDATA[ http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$ ]]> </encode> <decode> <![CDATA[ 12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537 ]]> </decode> <type>8</type> <lrcid>2829</lrcid> <flag>1</flag> </url> <durl> <encode> <![CDATA[ http://zhangmenshiting2.baidu.com/data2/music/7345405/aGVnaWlmbGaeomZzrZmmnJZvmGqXbHCbl2dsZ5qXaWqSlWpsmmdrb2mXamxpbXCclGNsmW2ba25mYmxtapmZcWqTWaGemnRoX2VkbWdvaGhoZmZramluOA$$ ]]> </encode> <decode> <![CDATA[ 7345405.mp3?xcode=e6b69cf593ea22ac78e1478e78479dc19e8e4650995cb99a&mid=0.31929107437537 ]]> </decode> <type>8</type> <lrcid>2829</lrcid> <flag>1</flag> </durl> <p2p> <hash>f98b6772aa97966550ec80617879becee0233bf4</hash> <url> <![CDATA[ ]]> </url> <type>mp3</type> <size>3778335</size> <bitrate>128</bitrate> </p2p> </result>
簡單的說明下,由于我們要做的只是獲取到歌曲的lrc歌詞地址,所以有用的只有2829這個標(biāo)簽。
而encode和decode里面的拼接起來就是mp3的下載地址,如本例的
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537
就是下載地址,不過音質(zhì)太差,有時間在研究下這個。
繼續(xù)說歌詞,注意lrcid標(biāo)簽里面的2829
http://box.zhangmen.baidu.com/bdlrc/ 這個是百度lrc歌詞存放地址,
然后本例的歌詞地址是http://box.zhangmen.baidu.com/bdlrc/28/2829.lrc
看到了吧,歌詞地址后面的兩個數(shù)字的計算方法是在lrcid除以100所獲得的整數(shù),就是第一個數(shù)字,然后第二個數(shù)字就是lrcid,然后后面加上后綴.lrc就搞定了
獲得lrc地址之后就簡單了,只要請求該地址,然后將獲取到的內(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('<lrcid>(.*?)</lrcid>',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!"
有用第一步請求所獲取到底是xml格式的,所以本來想著解析xml來獲取lrcid,但是在實現(xiàn)過程中遇到了各種問題,別的還容易,就在這一塊兒浪費的時間最長,糾結(jié)未果之后,只能改用正則表達式來獲取了。。。只能說明還是學(xué)藝不精呢
原文:逝去日子的博客 » 使用python掃描本地音樂并下載歌詞
相關(guān)文章
Linux CentOS Python開發(fā)環(huán)境搭建教程
這篇文章主要介紹了Linux CentOS Python開發(fā)環(huán)境搭建方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11Pytorch使用shuffle打亂數(shù)據(jù)的操作
這篇文章主要介紹了Pytorch使用shuffle打亂數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05python+JS?實現(xiàn)逆向?SMZDM?的登錄加密
這篇文章主要介紹了python+JS?實現(xiàn)逆向?SMZDM?的登錄加密,文章通過利用SMZDM平臺展開詳細的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05