import requests
from multiprocessing import Pool
class Music_QQ(object):def __init__(self):self.get_mid_url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?'self.get_vkey_url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?'self.get_song_url = 'http://isure.stream.qqmusic.qq.com/'self.get_mid_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36','Referer': 'https://y.qq.com/n/yqq/playlist/7281518382.html','Origin': 'https://y.qq.com','Sec-Fetch-Mode':'cors',}self.get_vkey_headers= {'Origin': 'https://y.qq.com','Referer': 'https://y.qq.com/portal/player.html','User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',}self.get_song_headers= {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',}self.parse()def parse(self):self.get_songmid()def get_songmid(self):song_infos = []params = {'type': '1','json': '1','utf8': '1','onlysong': '0','new_format': '1','disstid': '7010910521','g_tk': '5381','loginUin': '0','hostUin': '0','format': 'json','inCharset': 'utf8','outCharset': 'utf-8','notice': '0','platform': 'yqq.json','needNewCode': '0',}# base_url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&new_format=1&disstid=7281518382&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'response = requests.get(self.get_mid_url,headers= self.get_mid_headers,params=params,verify=False)# print(response.json())if response.status_code==200:for item in response.json()['cdlist'][0]['songlist']:mid = item['mid']name = item['name']song_infos.append((mid,name))# print(song_infos)#创建一个进程池对象pool = Pool(4)pool.map(self.get_vkeys,song_infos)def get_vkeys(self,songinfo):# print(songinfo)mid = songinfo[0]name = songinfo[1]params = {'g_tk': '5381','loginUin': '0','hostUin': '0','format': 'json','inCharset': 'utf8','outCharset': 'utf-8','notice': '0','platform': 'yqq.json','needNewCode': '0','data': '{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"2557522780","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"2557522780","songmid":["'+mid+'"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}',}response = requests.get(self.get_vkey_url,headers = self.get_vkey_headers,params=params,verify = False)# print(response.json())json_data = response.json()purl = json_data['req_0']['data']['midurlinfo'][0]['purl']# print(purl)self.down_load(purl,name)def down_load(self,purl,name):url = self.get_song_url+purlresponse = requests.get(url,headers = self.get_song_headers,verify = False)filename = 'C:\\Users\\Administrator\\Desktop\\music1\\{}.m4a'.format(name)try:print('开始下载:', name, sep='')with open(filename, 'wb') as fp:fp.write(response.content)print('下载成功:',name,sep='')except Exception:print('下载失败:',name,sep='')if __name__ == '__main__':Music_QQ()
运行结果报错