1、找到音乐列表
2、获取歌名、歌手、歌曲专辑、rid
headers = {
'Accept': 'application/json, text/plain, */*',
# 'Accept-Encoding': 'gzip, deflate, br',
# 'Accept-Language':' zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
# 'Connection': 'keep-alive',
'Cookie': '_ga=GA1.2.1883159436.1666154235; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1666154235,1667717526; _gid=GA1.2.936648438.1667717526; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1667717751; kw_token=FJHDGHIMPW9',
'csrf': 'FJHDGHIMPW9',
'Host': 'kuwo.cn',
'Referer': 'https://kuwo.cn/rankList',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
#url = 'http://www.kuwo.cn/api/v1/www/music/playUrl?mid=239211473&type=music&httpsStatus=1&reqId=32239821-4f68-11ed-a192-3df3a8c3d645'
#url = 'https://lq-sycdn.kuwo.cn/012e90577c5ad5a0680d66cbeb379137/634f7fad/resource/n3/86/28/2807588455.mp3'
url = 'https://kuwo.cn/api/www/bang/bang/musicList?bangId=93&pn=1&rn=30&httpsStatus=1&reqId=16a33a60-5da0-11ed-af0e-73dd1dd01588'
response = requests.get(url=url,headers=headers)
# print(response.json())
json_data = response.json()
data_list = json_data['data']['musicList']
# print(data_list)
for data in data_list:
song_name = data['name']
singer_name = data['artist']
album_name = data['album']
rid = data['rid']
print(song_name,singer_name,album_name,rid)
3、获取音乐详情链接
#用下面这个链接,因为有付费歌曲,导致报错,用&type=convert_url3&br=320kmp3
# song_info_url = f'https://kuwo.cn/api/v1/www/music/playUrl?mid={rid}&type=music&httpsStatus=1&reqId=792ab631-5da6-11ed-8673-f1406d1904fb'
'''流畅音质 128k
高品音质 192k
超品音质 320k
'''
song_info_url = f'https://kuwo.cn/api/v1/www/music/playUrl?mid={rid}&type=convert_url3&br=320kmp3'
music_url =requests.get(url=song_info_url).json()['data']['url']
print(music_url)
time.sleep(1)
music_data = requests.get(url=music_url).content
4、下载保存音乐
music_data = requests.get(url=music_url).content
with open(f'music\{singer_name}'+'.mp3','wb') as f:
f.write(music_data)
import requests
import json
import time
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json, text/plain, */*',
# 'Accept-Encoding': 'gzip, deflate, br',
# 'Accept-Language':' zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
# 'Connection': 'keep-alive',
'Cookie': '_ga=GA1.2.1883159436.1666154235; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1666154235,1667717526; _gid=GA1.2.936648438.1667717526; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1667717751; kw_token=FJHDGHIMPW9',
'csrf': 'FJHDGHIMPW9',
'Host': 'kuwo.cn',
'Referer': 'https://kuwo.cn/rankList',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
#url = 'http://www.kuwo.cn/api/v1/www/music/playUrl?mid=239211473&type=music&httpsStatus=1&reqId=32239821-4f68-11ed-a192-3df3a8c3d645'
#url = 'https://lq-sycdn.kuwo.cn/012e90577c5ad5a0680d66cbeb379137/634f7fad/resource/n3/86/28/2807588455.mp3'
url = 'https://kuwo.cn/api/www/bang/bang/musicList?bangId=93&pn=1&rn=30&httpsStatus=1&reqId=16a33a60-5da0-11ed-af0e-73dd1dd01588'
response = requests.get(url=url,headers=headers)
# print(response.json())
json_data = response.json()
data_list = json_data['data']['musicList']
# print(data_list)
for data in data_list:
song_name = data['name']
singer_name = data['artist']
album_name = data['album']
rid = data['rid']
print(song_name,singer_name,album_name,rid)
#用下面这个链接,因为有付费歌曲,导致报错,用&type=convert_url3&br=320kmp3
# song_info_url = f'https://kuwo.cn/api/v1/www/music/playUrl?mid={rid}&type=music&httpsStatus=1&reqId=792ab631-5da6-11ed-8673-f1406d1904fb'
'''流畅音质 128k
高品音质 192k
超品音质 320k
'''
song_info_url = f'https://kuwo.cn/api/v1/www/music/playUrl?mid={rid}&type=convert_url3&br=320kmp3'
music_url =requests.get(url=song_info_url).json()['data']['url']
print(music_url)
time.sleep(1)
music_data = requests.get(url=music_url).content
with open(f'music\{singer_name}'+'.mp3','wb') as f:
f.write(music_data)
5、以下是两个调试遇到的报错,记录一下