酷狗音乐现在只支持试听,可以运用代码实现歌曲的试听下载,代码内容仅作为参考学习,没有办法下载完整或者付费歌曲!
import json
import re
import requests
import hashlib
import time
import prettytable as pt
def get_data(date, music_id):
s = [
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
"appid=1014",
f"clienttime={date}"
"clientver=20000",
"dfid=1twPM20Knbvu3dbm6h1Zql59",
f"encode_album_audio_id={music_id}",
"mid=e1d0b0be0181e96e505ca798feed4ea8",
"platid=4",
"srcappid=2919",
"token=f3ff3dcdfafbb9f714d22b4a3292687ec0b250a1800d8e94be9baac1d69a3b24",
"userid=2293018052",
"uuid=e1d0b0be0181e96e505ca798feed4ea8",
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
]
string = ''.join(s)
MD5 = hashlib.md5()
MD5.update(string.encode('utf-8'))
signature = MD5.hexdigest()
return signature
def get_response(html, params=None):
headers = {
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0'
}
response = requests.get(html, params=params, headers=headers)
return response
def get_info(music_id):
date = int(time.time() * 1000)
signature = get_data(date, music_id)
url = 'https://wwwapi.kugou.com/play/songinfo'
data = {
'srcappid': '2919',
'clientver': '20000',
'clienttime': date,
'mid': 'e1d0b0be0181e96e505ca798feed4ea8',
'uuid': 'e1d0b0be0181e96e505ca798feed4ea8',
'dfid': '1twPM20Knbvu3dbm6h1Zql59',
'appid': '1014',
'platid': '4',
'encode_album_audio_id': music_id,
'token': 'f3ff3dcdfafbb9f714d22b4a3292687ec0b250a1800d8e94be9baac1d69a3b24',
'userid': '2293018052',
'signature': signature
}
json_data = get_response(url, data).json()
audio_name = json_data['data']['audio_name']
play_url = json_data['data']['play_url']
return audio_name, play_url
def save(audio_name, play_url):
content = get_response(play_url).content
with open('wy_music\\' + audio_name + '.mp3', mode='wb') as f:
f.write(content)
print(audio_name)
print('下载成功!')
def search_data(date, word):
s = [
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
"appid=1014",
"bitrate=0",
"callback=callback123",
f"clienttime={date}",
"clientver=1000",
"dfid=1twPM20Knbvu3dbm6h1Zql59",
"filter=10",
"inputtype=0",
"iscorrection=1",
"isfuzzy=0",
f"keyword={word}",
"mid=e1d0b0be0181e96e505ca798feed4ea8",
"page=1",
"pagesize=30",
"platform=WebFilter",
"privilege_filter=0",
"srcappid=2919",
"token=f3ff3dcdfafbb9f714d22b4a3292687ec0b250a1800d8e94be9baac1d69a3b24",
"userid=2293018052",
"uuid=e1d0b0be0181e96e505ca798feed4ea8",
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
]
string = ''.join(s)
MD5 = hashlib.md5()
MD5.update(string.encode('utf-8'))
signature = MD5.hexdigest()
return signature
def search_info(word):
date = int(time.time() * 1000)
signature = search_data(date, word)
search_url = 'https://complexsearch.kugou.com/v2/search/song'
data_ = {
'callback': 'callback123',
'srcappid': '2919',
'clientver': '1000',
'clienttime': date,
'mid': 'e1d0b0be0181e96e505ca798feed4ea8',
'uuid': 'e1d0b0be0181e96e505ca798feed4ea8',
'dfid': '1twPM20Knbvu3dbm6h1Zql59',
'keyword': word,
'page': '1',
'pagesize': '30',
'bitrate': '0',
'isfuzzy': '0',
'inputtype': '0',
'platform': 'WebFilter',
'userid': '2293018052',
'iscorrection': '1',
'privilege_filter': '0',
'filter': '10',
'token': 'f3ff3dcdfafbb9f714d22b4a3292687ec0b250a1800d8e94be9baac1d69a3b24',
'appid': '1014',
'signature': signature
}
html_data = get_response(search_url, data_).text
info = re.findall('callback123\((.*)', html_data)[0].replace(')', '')
json_data_ = json.loads(info)
tb = pt.PrettyTable()
tb.field_names = [
'序号',
'歌手',
'歌名',
'专辑',
'ID',
]
info_list = []
num = 0
for index in json_data_['data']['lists']:
AlbumName = index['AlbumName']
EMixSongID = index['EMixSongID']
SingerName = index['SingerName']
SongName = index['SongName']
dit = {
'序号': num,
'歌手': SingerName,
'歌名': SongName,
'专辑': AlbumName,
'ID': EMixSongID
}
info_list.append(dit)
tb.add_row([num, SingerName, SongName, AlbumName, EMixSongID])
num += 1
print(tb)
return info_list
def main():
while True:
word = input('请输入你要搜索的歌曲/歌手(00退出): ')
if word == '00':
break
info_list = search_info(word)
page = input('请输入你要下载的歌曲序号:')
music_id = info_list[int(page)]['ID']
name, music_url = get_info(music_id)
save(name, music_url)
main()