python *me music

import requests, time

def music():
key_name = input("输入要下载的歌曲:")
num = input("第几页:")

url1 = 'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn={}&rn=30&httpsStatus=1&reqId=da11ad51-d211-11ea-b197-8bff3b9f83d2e'.format(key_name, num)

header1 = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Cookie': '_ga=GA1.2.1500987479.1595755923; _gid=GA1.2.568444838.1596065504; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1595755923,1596065505; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1596078189; _gat=1; kw_token=IJATWHHGI8',
'csrf': 'IJATWHHGI8',
'Host': 'www.kuwo.cn',
'Referer': 'http://www.kuwo.cn/search/list?key=%E6%A2%A6%E7%9A%84%E5%9C%B0%E6%96%B9',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',

}

response = requests.get(url1, headers=header1) # 访问歌曲列表
text1 = response.json()

list_music = text1['data']['list']
# print(list_music)
list_artist = []
list_rid = []
list_name = []
for i in list_music:
list_artist.append(i['artist'])
list_rid.append(i['rid'])
list_name.append(i['name'])

choose = 0
for j in range(1, len(list_rid)):
print(j, list_name[j], list_artist[j])

choose = int(input("请输入对应序列号即可下载!\n"))

rid = list_rid[choose]
title = list_name[choose]
arlist = list_artist[choose]

url = 'http://www.kuwo.cn/url?'

header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4421.5 Safari/537.36",
'Accept': "application/json, text/plain",
"Connection": "keep-alive",
"Accept-Language": "zh-CN,zh;q=0.9",
}

param = {
'format': 'mp3',
'response': 'url',
'type': 'convert_url3',
'br': '128kmp3',
'from': 'web',
'httpsStatus': '1',
't': '1615346672099',
'reqId': '214d1a40-8150-11eb-8602-513783b96ef3',
'rid': rid,
}

res = requests.get(url=url, headers=header, params=param)
res.encoding = 'utf-8'

text = res.json()

r = requests.get(url=text['url'], headers=header, params=param).content
with open('./' + title + '.mp3', 'wb') as code:
code.write(r)
print(arlist + title + " 下载完毕!")
time.sleep(10)

music()

posted @ 2021-03-30 18:30  tevien  阅读(61)  评论(0)    收藏  举报