圆月山庄资源网 Design By www.vgjia.com

这次这个真的是干货哦,昨晚弄了半晚上,,,,从8点吃完饭就开始写,一直到了快12点才弄好,,,新手,伤不起呀。。。。
先简单的说下吧,百度提供了一个音乐搜索的api,你想百度请求类似于

http://box.zhangmen.baidu.com/x"htmlcode">
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"htmlcode">
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3"htmlcode">
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"+b+"$$"+a+"$$$$ "
  
  the_file.write(song_url+'\n')
  page = urllib2.urlopen(song_url).read()
  print page
  theid = 0
  
  lrcid = re.compile('<lrcid>(.*"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歌词

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com