描述
可以下载QQ音乐免费音乐和绿钻音乐,但是不能下载付费音乐。
运行程序后会在同级目录自动创建名为:歌曲下载的文件夹,下载的文件将被放置在此文件夹
如果下载的文件大小为1KB,则表示下载失败,这首歌不支持下载
源代码如下:
# -*- coding: utf-8 -*-
import json
import os
import requestsheaders = {'Origin': 'https://y.qq.com','Referer': 'https://y.qq.com/portal/search.html','Sec-Fetch-Mode': 'cors','User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
}def get_music_info():music_info_list = []name = input('请输入歌手或歌曲:') # input函数 输入 做用户交互 使用的page = input('请输入页码:')num = input('请输入当前页码需要返回的数据条数:')url = f'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?p={page}&n={num}&w={name}'response = requests.get(url).text # 获取到的是字符串# 将response切分成json格式 类似字典 但是现在还是字符串music_json = response[9:-1]# json转字典music_data = json.loads(music_json) # 转换成 字典# print(music_data)music_list = music_data['data']['song']['list']for music in music_list:music_name = music['songname'] # 歌曲的名字singer_name = music['singer'][0]['name'] # 歌手的名字songmid = music['songmid']media_mid = music['media_mid']music_info_list.append((music_name, singer_name, songmid, media_mid))return music_info_list# 获取vkey
def get_purl(music_info_list):music_data = []for music in music_info_list:music_name = music[0]singer_name = music[1]songmid = music[2]url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"8846039534","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"8846039534","songmid":["%s"],"songtype":[0],"uin":"1152921504784213523","loginflag":1,"platform":"20"}},"comm":{"uin":"1152921504784213523","format":"json","ct":24,"cv":0}}' % songmidresponse = requests.get(url).json() # 如果你获取的数据 是 {} .json() 他会直接帮我们转换成字典purl = response['req_0']['data']['midurlinfo'][0]['purl']full_media_url = 'http://dl.stream.qqmusic.qq.com/' + purlmusic_data.append({'music_name': music_name,'singer_name': singer_name,'full_media_url': full_media_url})return music_datadef save_music_mp3(music_data):if not os.path.exists('歌曲下载'): # 判断是否有歌曲下载文件夹os.mkdir('歌曲下载') # 如果没有创建 歌曲下载文件夹for music in music_data:music_name = music['music_name']singer_name = music['singer_name']full_url = music['full_media_url']music_response = requests.get(full_url, headers=headers).contentwith open('歌曲下载/%s-%s.mp3' % (music_name, singer_name), 'wb')as fp:fp.write(music_response)print('[%s]保存成功!' % music_name)if __name__ == '__main__':music_info_list = get_music_info()music_data = get_purl(music_info_list)save_music_mp3(music_data)
运行结果: