根据输入歌曲名字,会下载搜索结果的所有音乐
import hashlib
import json
import re
import time
import requests
# 网易云音乐爬取
def name_sign(name):
t = int(time.time() * 1000)
params = [
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
"appid=1014",
"bitrate=0",
"callback=callback123",
"clienttime={}".format(t),
"clientver=1000",
"dfid=3ewPkv44v5es1H8fDD3mS5Pb",
"filter=10",
"inputtype=0",
"iscorrection=1",
"isfuzzy=0",
"keyword={}".format(name),
"mid=6ff63a382693083fdbb97e121c223e7e",
"page=1",
"pagesize=30",
"platform=WebFilter",
"privilege_filter=0",
"srcappid=2919",
"token=",
"userid=0",
"uuid=6ff63a382693083fdbb97e121c223e7e",
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
]
sign = hashlib.md5(''.join(params).encode('utf-8')).hexdigest()
print(sign)
return sign
def name_class(name):
sign = name_sign(name)
t = int(time.time() * 1000)
data = {
'callback': 'callback123', # 确保'callback'有实际的字符串值
'srcappid': '2919',
'clientver': '1000',
'clienttime': t, # 使用计算出的时间戳
'mid': '6ff63a382693083fdbb97e121c223e7e',
'uuid': '6ff63a382693083fdbb97e121c223e7e',
'dfid': '3ewPkv44v5es1H8fDD3mS5Pb',
'keyword': name,
'page': '1',
'pagesize': '30',
'bitrate': '0',
'isfuzzy': '0',
'inputtype': '0',
'platform': 'WebFilter',
'userid': '0',
'iscorrection': '1',
'privilege_filter': '0',
'filter': '10',
'token': '', # 明确赋值为空字符串
'appid': '1014',
'signature': sign # 使用生成的签名
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
try:
# print(data)
response = requests.get(url='https://complexsearch.kugou.com/v2/search/song?', headers=headers, params=data)
callback_dict = re.findall(r'callback123\((.*)\)', response.text)[0]
jsurl = json.loads(callback_dict)
i=len(jsurl['data']['lists'])
for item in jsurl['data']['lists']:
fileName = item['FileName']
eMixSongID = item['EMixSongID']
fetch_music_data(eMixSongID)
print(fileName + str(eMixSongID))
except Exception as e:
print("请求过程中发生错误:", e)
def generate_sign(music_id):
t = int(time.time() * 1000)
params = [
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
"appid=1014",
"clienttime={}".format(t),
"clientver=20000",
"dfid=3ewPkv44v5es1H8fDD3mS5Pb",
"encode_album_audio_id={}".format(music_id),
"mid=6ff63a382693083fdbb97e121c223e7e",
"platid=4",
"srcappid=2919",
"token=",
"userid=0",
"uuid=6ff63a382693083fdbb97e121c223e7e",
"NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
]
sign = hashlib.md5(''.join(params).encode('utf-8')).hexdigest()
return sign
def fetch_music_data(music_id):
sign = generate_sign(music_id)
t = int(time.time() * 1000)
data = {
'srcappid': '2919',
'clientver': '20000',
'clienttime': t,
'mid': '6ff63a382693083fdbb97e121c223e7e',
'uuid': '6ff63a382693083fdbb97e121c223e7e',
'dfid': '3ewPkv44v5es1H8fDD3mS5Pb',
'appid': '1014',
'platid': '4',
'encode_album_audio_id': music_id,
'token': '',
'userid': 0,
'signature': sign,
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
# url='https://wwwapi.kugou.com/play/songinfo?'
try:
response = requests.get(url='https://wwwapi.kugou.com/play/songinfo?', headers=headers, params=data)
if response.status_code == 200:
jsurl=response.json()
play_url = jsurl['data']['play_url']
print(play_url)
download_music(play_url)
else:
print("请求失败,状态码:", response.status_code)
except Exception as e:
print("请求过程中发生错误:", e)
def download_music(play_url, filename=None):
"""
下载音乐文件并保存到当前目录。
:param play_url: 音乐播放地址
:param filename: 保存时使用的文件名,默认为URL的最后一部分
"""
# 如果没有指定文件名,则从URL中提取
if not filename:
filename = play_url.split("/")[-1]
try:
# 发起GET请求下载文件内容
response = requests.get(play_url, stream=True)
# 检查请求是否成功
if response.status_code == 200:
# 以二进制写模式打开文件
with open(filename, 'wb') as file:
# 分块写入文件
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print(f"音乐文件已成功保存为:{filename}")
else:
print("下载失败,HTTP状态码:", response.status_code)
except Exception as e:
print(f"下载过程中发生错误:{e}")
if __name__ == "__main__":
name=input("音乐歌手:")
# music_id = 'bmu1rt55'
name_class(name)
# fetch_music_data(music_id)