python中使用百度音樂(lè)搜索的api下載指定歌曲的lrc歌詞
這次這個(gè)真的是干貨哦,昨晚弄了半晚上,,,,從8點(diǎn)吃完飯就開(kāi)始寫(xiě),一直到了快12點(diǎn)才弄好,,,新手,傷不起呀。。。。
先簡(jiǎn)單的說(shuō)下吧,百度提供了一個(gè)音樂(lè)搜索的api,你想百度請(qǐng)求類(lèi)似于
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. <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>
簡(jiǎn)單的說(shuō)明下,由于我們要做的只是獲取到歌曲的lrc歌詞地址,所以有用的只有2829這個(gè)標(biāo)簽。
而encode和decode里面的拼接起來(lái)就是mp3的下載地址,如本例的
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537
就是下載地址,不過(guò)音質(zhì)太差,有時(shí)間在研究下這個(gè)。
繼續(xù)說(shuō)歌詞,注意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)容寫(xiě)入文件就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!"
有用第一步請(qǐng)求所獲取到底是xml格式的,所以本來(lái)想著解析xml來(lái)獲取lrcid,但是在實(shí)現(xiàn)過(guò)程中遇到了各種問(wèn)題,別的還容易,就在這一塊兒浪費(fèi)的時(shí)間最長(zhǎng),糾結(jié)未果之后,只能改用正則表達(dá)式來(lái)獲取了。。。只能說(shuō)明還是學(xué)藝不精呢
原文:逝去日子的博客 » 使用python掃描本地音樂(lè)并下載歌詞
相關(guān)文章
python實(shí)現(xiàn)挑選出來(lái)100以?xún)?nèi)的質(zhì)數(shù)
這篇文章主要介紹了python實(shí)現(xiàn)挑選出來(lái)100以?xún)?nèi)的質(zhì)數(shù),由于是新手,思路有限,如有問(wèn)題,還請(qǐng)大家指正,需要的朋友可以參考下2015-03-03
python3.0 模擬用戶(hù)登錄,三次錯(cuò)誤鎖定的實(shí)例
下面小編就為大家?guī)?lái)一篇python3.0 模擬用戶(hù)登錄,三次錯(cuò)誤鎖定的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
詳解Python并發(fā)編程之從性能角度來(lái)初探并發(fā)編程
這篇文章主要介紹了詳解Python并發(fā)編程之從性能角度來(lái)初探并發(fā)編程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
pandas map(),apply(),applymap()區(qū)別解析
這篇文章主要介紹了pandas map(),apply(),applymap()區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
python實(shí)現(xiàn)簡(jiǎn)單的TCP代理服務(wù)器
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單的TCP代理服務(wù)器,包含了完整的實(shí)現(xiàn)過(guò)程及對(duì)應(yīng)的源碼與說(shuō)明文檔下載,非常具有參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10
TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)
這篇文章主要介紹了TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫(kù)連接池
這篇文章主要介紹了Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫(kù)連接池,Python 連接 PostgreSQL 是主要有兩個(gè)包, py-postgresql 和 psycopg2 , 而本文的實(shí)例將使用后者,感興趣的小伙伴可以參考一下2021-10-10

