一个调用网易云音乐API 下载想要歌曲的几行字

# -*- coding:utf-8 -*-

import urllib
import os,mp3play
import json
import sys
import time
reload(sys)
sys.setdefaultencoding('gb18030')

def getURL_of_music(json_url):
    url = json_url
    response = urllib.urlopen(url)
    content = response.read().decode('utf-8')
    #print content
    js = json.loads(content)
    k = u"audio"
    #print js['result']['songs'][0]
    music_url = js['result']['songs'][0][k]
    return music_url


def download_music(save_path,songName,url_org):
    url = getURL_of_music(url_org)
    if not os.path.isdir(save_path):
        os.mkdir(save_path)
    save_music_name = save_path.decode('utf-8')+"\\"+songName.decode('utf-8')+u".mp3"
    if not os.path.exists(save_music_name): 
        #print save_music_name
        urllib.urlretrieve(url,save_music_name)
    return save_music_name


def play_music(music_path):
    file_mp3 = music_path
    mp3 = mp3play.load(file_mp3)
    mp3.play()
    time.sleep(mp3.seconds())
    mp3.stop()
    
    

if __name__ == "__main__":
        
    music_name=raw_input("Please input music name of what you want to listen:").encode('utf-8')
    limit = "2"
    type ="1"
    url='http://s.music.163.com/search/get/'
    url = url+"?type="+type+"&limit="+limit+"&s="+music_name
    save_music = download_music('G:\Music\wangyi', music_name, url)
    flag = raw_input("Would you like to listen %s right now ?: (Y/N)"%music_name.decode('utf-8'))
    if flag=='Y':
        play_music(save_music)
    else:
        print "Are you kiding me?"
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值