import requests
pages = int(input('请输入爬取页数(一页30首歌):'))
singer = input('输入歌手:')
headers = {
'Accept': 'application/json, text/plain, /',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Cookie': '_ga=GA1.2.1792740187.1663770553; gid=b5ebe26a-f785-4497-a8d8-b4b12c7bd666; ad_dist=%25BA%25FE%25C4%25CF; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1663821731,1663821869,1663859385,1664039106; _gid=GA1.2.639206880.1664039106; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1664039442; kw_token=9688A83OKQ5',
'csrf': '9688A83OKQ5',
'Host': 'www.kuwo.cn',
'origin': 'https://www.kuwo.cn/',
'Referer': 'https://www.kuwo.cn/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.50',
}
for kk in range(1,pages+1):
music_list_url ='https://kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn={}&rn=30&httpsStatus=1&reqId=f229e0a1-3bba-11ed-94e2-5fcd5a860e51'.format(singer,kk)
rst = requests.get(url=music_list_url,headers=headers).json()
print(rst)
data = rst['data']['list']
for i in data:
song_name = i["name"]
song_id = i["rid"]
print(song_name,song_id)
viedo = 'http://www.kuwo.cn/api/v1/www/music/playUrl?mid={}&type=convert_url3&br=320kmp3'.format(song_id)
rsult = requests.get(url=viedo,headers=headers).json()
music_url = rsult['data']['url']
music = requests.get(url=music_url).content
with open('video/周杰伦/{}.mp3'.format(song_name),mode='wb')as f:
f.write(music)
print(f'-----正在下载{song_name}---')
print('--下载完成---')