网易云vip下载(仅供娱乐)

郑重声明:仅供娱乐
话不多说上代码:

#--*encoding:gbk*--

import requests
import json

class MyMusic(object):
    def __init__(self):
        # 设置请求头
        self.url = "https://api.imjad.cn/cloudmusic/"  # api
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",}
        self.params_search = {
            'type': 'search',
            'search_type': str(1),
            'limit': str(20),#返回20个搜索结果
            's': '',
            'offset': str(0)
        }
        self.params_download = {
            'type': 'song',
            'id': '',
            'br': str(320000),#高品质的音质
        }

    def SearchData(self, name):
        #响应的json数据
        self.params_search['s'] = name
        response = requests.get(url=self.url, headers=self.headers, params=self.params_search)
        html = response.json()
        return html

    def SearchList(self, name):
        #打印搜索列表
        html = self.SearchData(name)
        song_id = []
        song_name = []
        song_author = []
        for i in range(20):#params里设置的搜索返回20条数据
            song_id.append(html["result"]["songs"][i]['id'])
            song_name.append(html["result"]["songs"][i]['name'])
            song_author.append(html["result"]["songs"][i]['ar'][0]['name'])
            print('(ID:' + str(song_id[i]) + ')歌名:' + song_name[i] + ' ------ 歌手:' + song_author[i])

    def SearchSong(self, name, author):
        #匹配歌名和歌手,返回id号
        html = self.SearchData(name)
        for i in range(20):
            if (html["result"]["songs"][i]['name'] == name) and (html["result"]["songs"][i]['ar'][0]['name'] == author):
                return html["result"]["songs"][i]['id']

    def DownLoad(self, id):
        #下载
        self.params_download['id'] = id
        response = requests.get(url=self.url, headers=self.headers, params=self.params_download)
        url = response.json()['data'][0]['url']
        print("正在下载:" + self.params_search['s'])
        response = requests.get(url, headers=self.headers)
        filename =self.params_search['s'] + ".mp3"
        with open(filename, 'wb')as f:
            f.write(response.content)
            print("下载成功!")


    def run(self):
        print("@程序优化中,歌名歌手输入框出现繁体或片假名则不能正常运行\n"
              "@歌曲框输入exit退出程序\n"
              "@歌手框输入exit则放弃操作\n")
        while(True):
            try:
                name = input("请输入歌曲名称: ")
                if name == 'exit':
                    break
                print('======================搜索到的歌曲====================')
                self.SearchList(name)
                id = (input("请输入歌手名称或歌曲ID: "))
                if id == 'exit':
                    continue
                elif str.isdigit(id):
                #输入的是ID号,则直接下载
                    self.DownLoad(id)
                else:
                #输入的是歌手名字,匹配ID号
                    id = str(self.SearchSong(name, id))
                    self.DownLoad(id)
            except Exception:
                print("哎呀!下载出错了!可能原因如下\n" 
                      "1.您下载的歌热度不高哦\n" 
                      "2.请确保该歌曲在网易云搜索到\n" 
                      "3.程序崩溃了@_@")
                pass

t = MyMusic()
t.run()

©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值