import requests
import json,os
singer_list = ["陈明","辛晓琪","齐秦","羽泉","林志炫","杨宗纬","黄绮珊","彭佳慧","周晓欧","黄贯中","尚雯婕","沙宝亮","韦唯","韩磊","周笔畅","曹格","张宇","罗琦","邓紫棋","张杰","品冠","茜拉","满文军","动力火车","张靓颖","古巨基","胡彦斌","韩红","陈洁仪","黄丽玲","孙楠","李荣浩","李健","谭维维","郑淳元","李佳薇","萧煌奇"]
class QQmusic:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
self.sl = []
self.musicList = []
def getPage(self,url,headers):
res = requests.get(url,headers = headers)
res.encoding = 'utf-8'
return res
def getSongmid(self):
num = 99
for singer in singer_list:
name = singer
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?p=1&n=%d&w=%s'%(num,name)
res = self.getPage(url,headers=self.headers)
html = res.text
html = html[9:]
html = html[:-1]
js = json.loads(html)
songlist = js['data']['song']['list']
for song in songlist:
print(song)
songmid = song['songmid']
name = song['songname']
self.sl.append((name,songmid))
print('获取成功songmid')
def getVkey(self):
guid = "1282808556"
uin = "641043558"
for s in self.sl:
print('开始获取资源')
name = s[0]
songmid = s[1]
keyUrl = 'https://u.y.qq.com/cgi-bin/musicu.fcg?&data={"req":{"param":{"guid":" %s"}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"%s","songmid":["%s"],"uin":"%s"}},"comm":{"uin":%s}}'%(guid,guid,songmid,uin,uin)
res = self.getPage(keyUrl,headers=self.headers)
html = res.text
keyjs = json.loads(html)
try:
purl = keyjs['req_0']['data']['midurlinfo'][0]['purl']
except:
print('error, continue')
url = 'http://dl.stream.qqmusic.qq.com/' + purl
self.musicList.append((name,url))
print('资源地址获取成功')
def downloadMusic(self):
for m in self.musicList:
url = m[1]
res = self.getPage(url,headers=self.headers)
music = res.content
name = m[0] + '.mp3'
try:
with open(name, 'wb') as f:
f.write(music)
print('下载OK')
f.closed
except:
print("无法下载")
QQ = QQmusic()
QQ.getSongmid()
QQ.getVkey()
QQ.downloadMusic()