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中实现远程调用(RPC、RMI)简单例子
Apr 28 Python
浅谈Python中copy()方法的使用
May 21 Python
Python读取网页内容的方法
Jul 30 Python
python算法表示概念扫盲教程
Apr 13 Python
Python多层装饰器用法实例分析
Feb 09 Python
python的staticmethod与classmethod实现实例代码
Feb 11 Python
Python3 chardet模块查看编码格式的例子
Aug 14 Python
pygame实现俄罗斯方块游戏(对战篇1)
Oct 29 Python
pytorch:实现简单的GAN示例(MNIST数据集)
Jan 10 Python
Python生成六万个随机,唯一的8位数字和数字组成的随机字符串实例
Mar 03 Python
Django REST Swagger实现指定api参数
Jul 07 Python
python openCV自制绘画板
Oct 27 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下一个非常全面获取图象信息的函数
2008/11/20 PHP
CodeIgniter中实现泛域名解析
2014/07/19 PHP
php cli配置文件问题分析
2015/10/15 PHP
php PDO异常处理详解
2016/11/20 PHP
PHP实现QQ、微信和支付宝三合一收款码实例代码
2018/02/19 PHP
Laravel框架中缓存的使用方法分析
2019/09/06 PHP
php适配器模式简单应用示例
2019/10/23 PHP
IE和FireFox(FF)中js和css的不同
2009/04/13 Javascript
jquery 页眉单行信息滚动显示实现思路及代码
2014/06/26 Javascript
jQuery通用的全局遍历方法$.each()用法实例
2016/07/04 Javascript
lhgcalendar时间插件限制只能选择三个月的实现方法
2017/07/03 Javascript
jQuery访问浏览器本地存储cookie、localStorage和sessionStorage的基本用法
2017/10/20 jQuery
VUE2实现事件驱动弹窗示例
2017/10/21 Javascript
EasyUI的DataGrid绑定Json数据源的示例代码
2017/12/16 Javascript
JavaScript中使用import 和require打包后实现原理分析
2018/03/07 Javascript
Angular6 正则表达式允许输入部分中文字符
2018/09/10 Javascript
详解50行代码,Node爬虫练手项目
2019/04/22 Javascript
vue项目中将element-ui table表格写成组件的实现代码
2019/06/12 Javascript
js全屏事件fullscreenchange 实现全屏、退出全屏操作
2019/09/17 Javascript
vue新建项目并配置标准路由过程解析
2019/12/09 Javascript
如何修改Vue打包后文件的接口地址配置的方法
2020/04/22 Javascript
多种类型jQuery网页验证码插件代码实例
2021/01/09 jQuery
python基础教程之缩进介绍
2014/08/29 Python
Python实现国外赌场热门游戏Craps(双骰子)
2015/03/31 Python
详解Python Socket网络编程
2016/01/05 Python
详解Python 2.6 升级至 Python 2.7 的实践心得
2017/04/27 Python
python reduce 函数使用详解
2017/12/05 Python
python 实现将字典dict、列表list中的中文正常显示方法
2018/07/06 Python
python 数字类型和字符串类型的相互转换实例
2018/07/17 Python
python 顺时针打印矩阵的超简洁代码
2018/11/14 Python
Mavi牛仔裤美国官网:土耳其著名牛仔品牌
2016/09/24 全球购物
关于运动会的稿件
2014/02/02 职场文书
十八大感想感言
2014/02/10 职场文书
实施意见格式范本
2015/06/05 职场文书
《绝招》教学反思
2016/02/20 职场文书
python实现的人脸识别打卡系统
2021/05/08 Python