python中使用百度音乐搜索的api下载指定歌曲的lrc歌词
作者:junjie 时间:2023-06-14 06:23:07
这次这个真的是干货哦,昨晚弄了半晚上,,,,从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这个标签。
而encode和decode里面的拼接起来就是mp3的下载地址,如本例的
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537
就是下载地址,不过音质太差,有时间在研究下这个。
继续说歌词,注意lrcid标签里面的2829
http://box.zhangmen.baidu.com/bdlrc/ 这个是百度lrc歌词存放地址,
然后本例的歌词地址是http://box.zhangmen.baidu.com/bdlrc/28/2829.lrc
看到了吧,歌词地址后面的两个数字的计算方法是在lrcid除以100所获得的整数,就是第一个数字,然后第二个数字就是lrcid,然后后面加上后缀.lrc就搞定了
获得lrc地址之后就简单了,只要请求该地址,然后将获取到的内容写入文件就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,但是在实现过程中遇到了各种问题,别的还容易,就在这一块儿浪费的时间最长,纠结未果之后,只能改用正则表达式来获取了。。。只能说明还是学艺不精呢
原文:逝去日子的博客 » 使用python扫描本地音乐并下载歌词
标签:python,百度音乐索,下载歌曲,lrc歌词
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pycharm 如何查看某一函数源码的快捷键
2023-10-25 02:41:28
![](https://img.aspxhome.com/file/2023/4/69604_0s.png)
MySQL系列之三 基础篇
2024-01-25 21:09:28
如何爬取通过ajax加载数据的网站
2022-05-03 15:06:01
![](https://img.aspxhome.com/file/2023/7/94077_0s.png)
教你如何用python开发一款数字推盘小游戏
2021-11-03 23:22:14
![](https://img.aspxhome.com/file/2023/4/71404_0s.png)
Python如何读取、写入CSV数据
2022-02-17 14:03:31
python3 面向对象__类的内置属性与方法的实例代码
2023-03-24 10:03:19
MySQL中用户授权以及删除授权的方法
2024-01-27 13:25:03
oracle学习笔记(三)
2012-01-05 19:28:42
mysql 维护常用命令
2024-01-26 08:10:54
详解Python+Pyecharts实现漏斗图的绘制
2022-07-07 10:22:42
![](https://img.aspxhome.com/file/2023/2/135222_0s.png)
Python实现ATM系统
2021-10-17 05:20:46
SQLServer 游标简介与使用说明
2009-07-02 13:53:00
php查询mysql数据库并将结果保存到数组的方法
2023-07-20 17:47:32
自动完成autoComplete
2011-01-17 18:01:00
Vue自定义可以选择日期区间段的日历插件
2024-05-11 09:11:19
![](https://img.aspxhome.com/file/2023/5/126535_0s.jpg)
搞定MySQL数据库中文模糊检索问题
2007-09-17 12:36:00
PHP中soap的用法实例
2023-11-14 09:40:24
MySQL中的回表和索引覆盖示例详解
2024-01-20 11:37:16
![](https://img.aspxhome.com/file/2023/9/81039_0s.png)
ASP 多关键词查询实例代码
2011-04-11 11:14:00
Python地图四色原理的遗传算法着色实现
2022-10-07 20:57:45
![](https://img.aspxhome.com/file/2023/5/75955_0s.jpg)