import requests
import re#正则表达式
import json
import os#处理文件和目录
def get_name(singer):
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
#对应接口
params = {
'catZhida': '1',
'w': singer,
}
#设置字典
headers = {
'referer': 'https://y.qq.com/portal/search.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
#设置头文件
html = requests.get(url,headers=headers,params=params).text
#请求数据,并返回Unicode型的数据
content = re.compile('callback\((.*)\)').findall(html)[0]
#compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用
#从html这个实例中,从0开始匹配,匹配内容为【callback()】里的内容
content = json.loads(content)#对数据进行json解码
data = content.get('data')
song = data.get('song')
lists = song.get('list')
#获取data、song、list标签的数据
name = []
#声明一个列表
for list in lists:
singer = list.get('singer')[0].get('mid')#获取song——list——singer——mid的value,这个应该是歌手对应的唯一编号
name.append(singer)#在name中,增加这个歌手编号
name = name[0]#获取歌手列表中的第一个
return name
def get_html(name,singer):
url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg'
params = {
'singermid': name,
'order': 'listen',
'begin': '0',
'num': '30',
}
headers = {
'referer': 'https://y.qq.com/n/yqq/singer/003aQYLo2x8izP.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
html = requests.get(url,headers=headers,params=params).text
return html
def get_music(vkey,songname,filename,singer):
if vkey and songname:
url3 = 'http://dl.stream.qqmusic.qq.com/' + filename + '?vkey=' + vkey + '&guid=7133372870&uin=0&fromtag=66'
headers = {
'referer': 'https://y.qq.com/n/yqq/singer/003aQYLo2x8izP.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
music = requests.get(url3,headers=headers).content
dir = singer
if not os.path.exists(dir):
os.mkdir(dir)
with open(dir+'/'+songname+'.m4a','wb') as f:
f.write(music)
print(songname,'__',singer)
def get_vkey(strMediaMid,songmid,songname,singer):
if strMediaMid and songmid and songname :
url2 = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg'
params = {
'g_tk': '5381',
'jsonpCallback': 'MusicJsonCallback8571665793949388',
'loginUin': '0',
'hostUin': '0',
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': '0',
'platform': 'yqq',
'needNewCode': '0',
'cid': '205361747',
'callback': 'MusicJsonCallback8571665793949388',
'uin': '0',
'songmid': songmid,
'filename': 'C400'+ strMediaMid + '.m4a',
'guid': '7133372870'
}
headers = {
'referer': 'https://y.qq.com/portal/player.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
detail_html = requests.get(url2,headers=headers,params=params).text
vkey_disc = re.compile('MusicJsonCallback8571665793949388\((.*?)\)').findall(detail_html)[0]
vkey_disc = json.loads(vkey_disc)
data = vkey_disc['data']
items = data.get('items')[0]
vkey = items.get('vkey')
get_music(vkey,songname,'C400'+ strMediaMid + '.m4a',singer)
def get_list(detail_html,singer):
if detail_html:
lists = re.compile('data\":{\"list\":(.*?),\"singer_id',re.S).findall(detail_html)[0]
datas = json.loads(lists)
for data in datas:
musicData = data.get('musicData')#????没有这个key啊
strMediaMid = musicData.get('strMediaMid')
songmid = musicData.get('songmid')
songname = musicData.get('songname')
get_vkey(strMediaMid,songmid,songname,singer)
def main():
singer = input('请输入您想要下载的歌手:')
name = get_name(singer)
detail_html = get_html(name,singer)
get_list(detail_html,singer)
if __name__ == '__main__':
main()
网上抄来的从QQ音乐下载某个歌手歌曲
最新推荐文章于 2024-09-25 05:59:42 发布