import requests
import re
import json
csrf_token = 'https://music.163.com/weapi/cloudsearch/get/web?csrf_token=15e98867801b1eed30913907b2b441a2'
# params 主要是来源于https://music.163.com/weapi/cloudsearch/get/web?csrf_token 这个接口的请求参数的。
post_data = requests.post(
url = csrf_token,
data = {
"params": "s84pIZvJd0aeSaBK6TZukmKZXpYhh3QZcbaKuAwyCxS22UvGCiJXnncLGz5lzIM5vvny0V8WK0GDgzyYP2i5RhUb6X4SEDh198XfvfyOkq2+tX/tvv+OMFQYEOef87xkEhf9y88OOR4sERDNOoLhpH/DPOUTRPQATJqxiWWMjFHng/qAPouih8DC+VrTgzGfg6UVBe8lqwXuYA7TyEKPVKklKlS2OePVThJZvatSge+mczFnLNG+uOr4koDVn+tFFeggBPNqU4VdbmiWTNS75zGxRfHIj2kmkUj8Um6BXbjV836DDKt/Jv4M3WFQVb7+",
"encSecKey": "90fc98d467b3448ffb7e3e615640026d6db68cd75de63729ee1faf03604a27c7b8e7f26e1b7ed98dbf20b71390f94b8ae211034b2478ede3d69abf39c6fd48fcbaa6e00213b7c09e8fd2fd54cbcf00f3c4c1fbf161d5efb8f2fb33134c3f55ab8d13f143794232f4568ceb8b1ff9d51db521f91010fe52e6fe4368c3defb217b"
},
headers={
"Cookie":"放自己的CK"
}
).json()
print(post_data)
result = post_data['result']['songs']
for i in result:
name = i['name']
id = i['id']
print(name,id)
musicUrl = "http://music.163.com/song/media/outer/url?id={}.mp3".format(id)
res = requests.get(musicUrl)
if "audio" in res.headers['Content-Type']:
print("Successfully fetched the file!")
mp3 = res.content
with open("music/"+name+".mp3","wb") as f :
f.write(mp3)
print(name,"下载完成")
else:
print("Failed to fetch the file!")
具体的参数自己去抓请求去看,这里就不做过多演示了。
自己抓一下我请求中的连接,去修改一下参数就可以使用了。
运行结果如下