python中使用百度音乐搜索的api下载指定歌曲的lrc歌词


Posted in Python onJuly 18, 2014

这次这个真的是干货哦,昨晚弄了半晚上,,,,从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 相关文章推荐
Python Socket使用实例
Dec 18 Python
pytorch cnn 识别手写的字实现自建图片数据
May 20 Python
matplotlib 输出保存指定尺寸的图片方法
May 24 Python
python Flask 装饰器顺序问题解决
Aug 08 Python
对python实现二维函数高次拟合的示例详解
Dec 29 Python
详解numpy.meshgrid()方法使用
Aug 01 Python
python实现tail -f 功能
Jan 17 Python
python对接ihuyi实现短信验证码发送
May 10 Python
Python基于numpy模块实现回归预测
May 14 Python
pytorch掉坑记录:model.eval的作用说明
Jun 23 Python
基于Python编写一个计算器程序,实现简单的加减乘除和取余二元运算
Aug 05 Python
Pyinstaller打包Scrapy项目的实现步骤
Sep 22 Python
python采集博客中上传的QQ截图文件
Jul 18 #Python
Python下singleton模式的实现方法
Jul 16 #Python
python的迭代器与生成器实例详解
Jul 16 #Python
Python的内存泄漏及gc模块的使用分析
Jul 16 #Python
Python的垃圾回收机制深入分析
Jul 16 #Python
python中将字典转换成其json字符串
Jul 16 #Python
记录Django开发心得
Jul 16 #Python
You might like
php分页示例代码
2007/03/19 PHP
PHP SQLite类
2009/05/07 PHP
教你如何在CI框架中使用 .htaccess 隐藏url中index.php
2014/06/09 PHP
php中Y2K38的漏洞解决方法实例分析
2014/09/22 PHP
Yii2.0使用阿里云OSS的SDK上传图片、下载、删除图片示例
2017/09/20 PHP
PHP异步进程助手async-helper
2018/02/05 PHP
js 禁用浏览器的后退功能的简单方法
2008/12/10 Javascript
JavaScript子窗口ModalDialog中操作父窗口对像
2012/12/11 Javascript
js获取电脑分辨率的思路及操作
2013/11/22 Javascript
js日期范围初始化得到前一个月日期的方法
2015/05/05 Javascript
JavaScript类继承及实例化的方法
2015/07/25 Javascript
TinyMCE汉化及本地上传图片功能实例详解
2016/05/31 Javascript
如何解决IONIC页面底部被遮住无法向上滚动问题
2016/09/06 Javascript
jQuery在ie6下无法设置select选中的解决方法详解
2016/09/20 Javascript
jquery请求servlet实现ajax异步请求的示例
2017/06/03 jQuery
jQuery实现可编辑表格并生成json结果(实例代码)
2017/07/19 jQuery
jQuery ajax调用webservice注意事项
2017/10/08 jQuery
详解Webpack多环境代码打包的方法
2018/08/03 Javascript
JS与SQL方式随机生成高强度密码示例
2018/12/29 Javascript
js+canvas实现纸牌游戏
2020/03/16 Javascript
[01:01:22]VGJ.S vs OG 2018国际邀请赛淘汰赛BO3 第一场 8.22
2018/08/23 DOTA
[53:44]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Magma BO3 第一场 1月31日
2021/03/11 DOTA
研究Python的ORM框架中的SQLAlchemy库的映射关系
2015/04/25 Python
利用python+ffmpeg合并B站视频及格式转换的实例代码
2020/11/24 Python
工程预算与管理应届生求职信
2013/10/06 职场文书
五年级音乐教学反思
2014/02/06 职场文书
网络编辑岗位职责范本
2014/02/10 职场文书
行政部工作岗位职责范本
2014/03/05 职场文书
优秀毕业生推荐信范文
2014/03/07 职场文书
成绩单家长评语大全
2014/04/16 职场文书
体育比赛口号
2014/06/09 职场文书
学习十八大的心得体会
2014/09/01 职场文书
华山导游词
2015/02/03 职场文书
贷款工资证明范本
2015/06/12 职场文书
Python 如何利用ffmpeg 处理视频素材
2021/11/27 Python
德生BCL3000抢先使用感受和评价
2022/04/07 无线电