[原创] 网易云音乐数据提取

ruby crawler

2017-05-09 2860

抓取歌曲数据,根据关键词进行提取

最终效果展示:http://penkuoer.com/netease-music/dist/#/


require 'json'
require 'rest-client'
class NeteaseMusic
  # 根据关键字查询歌曲数据
  def get_songs_search(key_word)
    threads = []
    result = []
    lists = JSON.parse RestClient.post('http://music.163.com/api/search/get/',
                                       {s:key_word,limit:100,sub:false,type:1,offset:0})
    lists['result']['songs'].each do |s|
      tem = s['album']
      album = {title:tem['name'],pic:tem['artist']['img1v1Url'],a_id:tem['id']}
      threads << Thread.new do
        get_songs album
      end
      result << album
    end
    threads.each { |t| t.join } result
  end

  private
  # 根据歌单(专辑)id获取歌曲信息
  def get_songs(album)
    data = JSON.parse RestClient.get("http://music.163.com/api/album/#{album[:a_id]}",
                                     {Referer:'http://music.163.com'})
    result = []
    data['album']['songs'].each do |s|
      result << {url:s['mp3Url'],name:s['name']}
    end
    album['songs'] = result
  end
end