当前位置:主页 > 软件编程 > Python代码 >

python实现音乐下载器

时间:2020-10-16 21:59:18 | 栏目:Python代码 | 点击:

本文实例为大家分享了python音乐下载器的具体代码,供大家参考,具体内容如下

import requests 
import urllib 
import json 
from tkinter import * 
import tkinter.messagebox 
from tkinter.filedialog import askdirectory 
 
class QQMusic(): 
  def __init__(self): 
    self.interface = Tk() 
    self.interface.title('QQ音乐下载器') 
    self.SavePath = 'C:/Users' 
    self.entry = None 
    self.label = None 
  def DownloadMusic(self): 
    res1 = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w='+self.entry.get()) 
    jm1 = json.loads(res1.text.strip('callback()[]'))['data']['song']['list'][0] 
 
    mids=jm1['media_mid'] 
    songmids=jm1['songmid'] 
    songnames=jm1['songname'] 
    singers=jm1['singer'][0]['name'] 
 
    res2 = requests.get('https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?&\ 
    jsonpCallback=MusicJsonCallback&cid=205361747&songmid='+songmids+'&filename=C400'+mids+'.m4a&guid=6612300644') 
    jm2 = json.loads(res2.text) 
    vkey = jm2['data']['items'][0]['vkey'] 
    srcs='http://dl.stream.qqmusic.qq.com/C400'+mids+'.m4a?vkey='+vkey+'&guid=6612300644&uin=0&fromtag=66' 
    try: 
      urllib.request.urlretrieve(srcs,self.SavePath+'/'+songnames+'-'+singers+'.mp3')#.m4a 
      tkinter.messagebox.showinfo('完成','歌曲下载成功\n保存在'+self.SavePath) 
    except: 
      tkinter.messagebox.showerror('错误','下载出错,请重试!') 
  def ChangePath(self): 
    self.SavePath = askdirectory() 
    self.label["text"] = self.SavePath 
  def Run(self): 
    button = Button(self.interface,text='下载歌曲',command = self.DownloadMusic) 
    button.grid(row=0,column=1,padx=3,pady=2) 
    self.entry = Entry(self.interface,width=30) 
    self.entry.grid(row=0,column=0,padx=3,pady=2) 
    button1 = Button(self.interface,text='保存位置',command = self.ChangePath) 
    button1.grid(row=1,column=1,padx=3,pady=2) 
    self.label = Label(self.interface,text=self.SavePath) 
    self.label.grid(row=1,column=0,padx=3,pady=2) 
     
QQ = QQMusic() 
QQ.Run() 

可以实现音乐的快速制定路径下载。

您可能感兴趣的文章:

相关文章