Use the Baidu Music Search API in Python to download the LRC lyrics for the specified song

Source: Internet
Author: User
Tags cdata
This time this is really dry, oh, last night to get a half night,,, from 8 o ' eat the meal began to write, until the quick 12 o'clock to fix,, novice, hurt Ah ....
The first simple say, Baidu provides a music search API, you want to Baidu request similar to

http://box.zhangmen.baidu.com/x?op=12&count=1&title= best loss friend $$ Eason Chan $

Address, Baidu will give you an XML return, as shown below

This XML file does does appear to has any style information associated with it. The document tree is shown below.
 
  
   
    1
   
  
  
    
     
     
     8 2829 1 
      http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$  
 
     
     12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537 
 
     
 
 
 
 
 

   
  
  
    
     
     
     8 2829 1 
      http://zhangmenshiting2.baidu.com/data2/music/7345405/aGVnaWlmbGaeomZzrZmmnJZvmGqXbHCbl2dsZ5qXaWqSlWpsmmdrb2mXamxpbXCclGNsmW2ba25mYmxtapmZcWqTWaGemnRoX2VkbWdvaGhoZmZramluOA$$  
 
     
     7345405.mp3?xcode=e6b69cf593ea22ac78e1478e78479dc19e8e4650995cb99a&mid=0.31929107437537 
 
     
 
 
 
 
 

   
  
  
    
    
     f98b6772aa97966550ec80617879becee0233bf4
     
     
     mp3 3778335 
       
 
     
     
    
 
 
 
 
 

    -
  
 

Simply explained below, since all we have to do is get to the song's LRC lyrics address, so useful only 2829 of this tag.
and encode and decode inside the stitching up is mp3 download address, as in this example

http://zhangmenshiting.baidu.com/data2/music/12762845/ Ymrqamdua21fn6nndk6ap5wxcjlrmg1xljhobwibmgpjk5ztmwizcwrjz5lqbgyelgkwlztubgljz5lka2uanwsxy1qin5t1ywbmzw5ocglhawdnbgtqbze $12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537

is the download address, but the sound quality is too poor, there is time to study this.
Keep saying the lyrics, note the 2829 in the lrcid tag.
http://box.zhangmen.baidu.com/bdlrc/This is Baidu LRC lyrics storage address,
Then the lyrics address of this example is HTTP://BOX.ZHANGMEN.BAIDU.COM/BDLRC/28/2829.LRC
See, the two numbers after the lyrics address are calculated by dividing lrcid by 100, which is the first number, and then the second number is lrcid, followed by a suffix. The LRC is done.
It's easy to get the LRC address, just request the address, and then write the retrieved content to the file.
Okay, that's probably it, here's the code:

Import osimport os.pathimport reimport eyed3import urllib2import urllibfrom urllib import urlencodeimport sys import Osrel Oad (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 = t He_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 (' 
 
  
   
 (.*?)

  
 ', 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 (Fir Stid) + "/" +theid+ ". LRC" Print Lrcurl LRC = Urllib2.urlopen (Lrcurl). Read () if (Lrc.find (' html ') = =-1): Lrcfile = open (LR   c_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!"

Useful first step request is obtained in the end is XML format, so originally wanted to parse XML to get lrcid, but in the implementation process encountered a variety of problems, other also easy, in this together wasted time the longest, tangled not fruit, can only use regular expression to get ... Just to show or not to be a fine apprentice.

Original: Blog of the Lost days» Use Python to scan local music and download lyrics

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.