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中的多重继承实例讲解
Sep 28 Python
Python通过poll实现异步IO的方法
Jun 04 Python
python实现自动发送邮件发送多人、群发、多附件的示例
Jan 23 Python
python实现支付宝当面付(扫码支付)功能
May 30 Python
Django的models模型的具体使用
Jul 15 Python
wxpython多线程防假死与线程间传递消息实例详解
Dec 13 Python
对tensorflow中的strides参数使用详解
Jan 04 Python
Python Selenium参数配置方法解析
Jan 19 Python
基于python实现上传文件到OSS代码实例
May 09 Python
python requests模块的使用示例
Apr 07 Python
PyQt5爬取12306车票信息程序的实现
May 14 Python
python+pytest接口自动化之token关联登录的实现
Apr 06 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的远程图片抓取函数分享
2013/09/25 PHP
基于Codeigniter框架实现的student信息系统站点动态发布功能详解
2017/03/23 PHP
JavaScript 事件参考手册
2008/12/24 Javascript
关于js遍历表格的实例
2013/07/10 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
JavaScript作用域链使用介绍
2013/08/29 Javascript
jquery中插件实现自动添加用户的具体代码
2013/11/15 Javascript
模拟用户点击弹出新页面不会被浏览器拦截
2014/04/08 Javascript
JavaScript中用字面量创建对象介绍
2014/12/31 Javascript
js点击选择文本的方法
2015/02/09 Javascript
jquery弹出遮掩层效果【附实例代码】
2016/04/28 Javascript
移动端界面的适配
2017/01/11 Javascript
Canvas 绘制粒子动画背景
2017/02/15 Javascript
VUE + UEditor 单图片跨域上传功能的实现方法
2018/02/08 Javascript
vue实现移动端input上传视频、音频
2020/08/18 Javascript
[03:22]DOTA2超级联赛专访单车:找到属于自己的英雄
2013/06/08 DOTA
[02:20]2014DOTA2西雅图邀请赛 MVP外卡赛首胜采访
2014/07/09 DOTA
Python正则表达式如何进行字符串替换实例
2016/12/28 Python
Win10+GPU版Pytorch1.1安装的安装步骤
2019/09/27 Python
wxPython色环电阻计算器
2019/11/18 Python
pytorch之ImageFolder使用详解
2020/01/06 Python
Python的赋值、深拷贝与浅拷贝的区别详解
2020/02/12 Python
matplotlib.pyplot.matshow 矩阵可视化实例
2020/06/16 Python
canvas学习笔记之绘制简单路径
2019/01/28 HTML / CSS
HTML5安全介绍之内容安全策略(CSP)简介
2012/07/10 HTML / CSS
html5 利用canvas实现超级玛丽简单动画
2013/09/06 HTML / CSS
尤为Wconcept中国官网:韩国设计师品牌服饰
2019/01/10 全球购物
英国豪华家具和家居用品购物网站:Teddy Beau
2020/10/12 全球购物
写给保洁员表扬信
2014/01/08 职场文书
乡镇办公室工作决心书
2014/03/11 职场文书
杜甫草堂导游词
2015/02/03 职场文书
个人收入证明范本
2015/06/12 职场文书
大学校园招聘会感想
2015/08/10 职场文书
《月球之谜》教学反思
2016/02/20 职场文书
golang正则之命名分组方式
2021/04/25 Golang
vue实现无缝轮播效果(跑马灯)
2021/05/14 Vue.js