import requests
def wy_music():
url = f'https://music.163.com/weapi/cloudsearch/get/web?csrf_token=0295f1fb7cf2e071d2417e47cf69f80c'
headers ={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
'cookie': 'NTES_P_UTID=Qa2AOfZX0j1UiRmj82XdAu0bGewKtPZ1|1630462183; P_INFO=feng_qwq@163.com|1630462183|0|mail163|00&99|not_found&1630330762&mailmaster_ios#shh&null#10#0#0|&0|mailmaster_ios|feng_qwq@163.com; nts_mail_user=feng_qwq@163.com:-1:1; _iuqxldmzr_=32; _ntes_nnid=bb83c471dafecf70353d36a2a1ce2044,1630462191047; _ntes_nuid=bb83c471dafecf70353d36a2a1ce2044; NMTID=00O1eGAqzrEu9Tu60lysCMb0vK2uIIAAAF7nx6mBw; WNMCID=ftjgmy.1630462191296.01.0; WEVNSM=1.0.0; WM_TID=EiI0VFLIen5FQUVABQZq3tPvQu2JOhQW; timing_user_id=time_8lFiicFQtG; WM_NI=rF6%2FWdTcKZblhwpwtCVk0MxtbWVU9vMCJsm2goLkMDZKiJrWxtGltfzWfkKzkW%2BJX7kZTshlccb1LDXtEnFNLYSSihmlyV9kLD66EF2ZXReJ3O4kE3OEIL6YI6IphKlbYTQ%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee9ab643bcf088b5f66bb6928bb2d85e939f9f84b53f8caf8bd0b54598f1f7aeca2af0fea7c3b92ab29afad0d67098bd85bbe168aca9c098f74996b500aefb80a2899ab6bc7e8f9d8694f03aa5b1848cd0509a95a7abf15dbcac898df76eba89aa8cca72a989e5abdb3da9869c94eb68a1bdaed2cf509cac8b8fc54690879db4ec64afb4b7b7bc61909cfad7c84ef28698b3e842f191fc8cc8398d9b87adb5538bea8791eb41b0be97b5d437e2a3; playerid=98119178; MUSIC_A_T=1539514560999; __csrf=0295f1fb7cf2e071d2417e47cf69f80c; __remember_me=true; MUSIC_U=45cb41c40210d38577e5460e2bd5b84779eefa4c89544846a0a46c577566995e993166e004087dd3d78b6050a17a35e705925a4e6992f61dfe3f0151024f9e31; MUSIC_R_T=1539514668634; ntes_kaola_ad=1; JSESSIONID-WYYY=6uSdt0E4zrNPaQ4Q%5C%2F74S%2FK8rTTOqEd3C9hp8wOI%2B5zC0bbzhEhs%5CWStWyQoNnakcF%2F5SuK4kKcWAMYOPovp5E%2BUpzf2Pov34AsQPjruIfGCCHpPrAo6XUdAWyreV0qr9H%5CjXHJqyjNdPvXBDncliUqboJshnHa3y0o4abkvT3menlER%3A1634124608642',
'referer': 'https://music.163.com/search/'
}
data = {
"params": "V3/mzHAebXKO3q7wOIUbi13vTEIhos2k/ccqS2Pe5AcmS9T7UUHvZnTYHPAITWDKXGyU5uz/68r2/LV2DJ4cXrpXO5US9BKXqOwiuBvn3awxlAp+f2CLD6m5iGUJpPjp0PG9ILfMceDW9iKLIUY6+m/in6tZGcgOqR7d/At0MRMLiOQMf3Sy/DYEfgC/ufQeEl+v5dN3VZS1BagYE7KtwfOvtnXL7eK4Kr4k0rLIWiiTb+2R3O8TKDkQ4LkqWy60rhQJC96z7kPg7kgNyJwKSCe+VevOmmyC/tG6RmOQg3vXk+aGosNTzxHY2alj0f/6",
"encSecKey": "de7bcb83bedcf63780d76e7a6d727c316121d6ae1d2edee3b0cb5ae6b778b29089a989edb8bcc92e79e0300ca557b3c43a3e5ea548e96f800454d365190cb31db3c745fdb316a2628db5922aca864ac6ea9546f9277518182689ed86614e39aafedebb7a2f04ea508a2da468db871f4d5b6099c81adf1abac37d052d8ceb1138",
}
response = requests.post(url,headers=headers,data=data).text
import json
reslist = json.loads(response)
print(reslist)
for i in range(0,30):
x = reslist['result']['songs'][i]["mv"]
name = reslist['result']['songs'][i]['name']
# print(type(x))
if x != 0:
url ="https://music.163.com/mv?id={}".format(x)
# print(url)
headers={
'authority': 'music.163.com',
'method': 'GET',
'path': '/',
'scheme': 'https',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
'cookie': 'NTES_P_UTID=Qa2AOfZX0j1UiRmj82XdAu0bGewKtPZ1|1630462183; P_INFO=feng_qwq@163.com|1630462183|0|mail163|00&99|not_found&1630330762&mailmaster_ios#shh&null#10#0#0|&0|mailmaster_ios|feng_qwq@163.com; nts_mail_user=feng_qwq@163.com:-1:1; _iuqxldmzr_=32; _ntes_nnid=bb83c471dafecf70353d36a2a1ce2044,1630462191047; _ntes_nuid=bb83c471dafecf70353d36a2a1ce2044; NMTID=00O1eGAqzrEu9Tu60lysCMb0vK2uIIAAAF7nx6mBw; WNMCID=ftjgmy.1630462191296.01.0; WEVNSM=1.0.0; WM_TID=EiI0VFLIen5FQUVABQZq3tPvQu2JOhQW; timing_user_id=time_8lFiicFQtG; MUSIC_A_T=1539514560999; __csrf=0295f1fb7cf2e071d2417e47cf69f80c; __remember_me=true; MUSIC_U=45cb41c40210d38577e5460e2bd5b84779eefa4c89544846a0a46c577566995e993166e004087dd3d78b6050a17a35e705925a4e6992f61dfe3f0151024f9e31; MUSIC_R_T=1539514668634; ntes_kaola_ad=1; JSESSIONID-WYYY=t9%2BuXSvZ2RDOuC8hS7skz8PvXm%5C00dD7yJ0fXp6uRHMXq%2FP27YxuNXD0gOc%5CHBdGmMh42avZqGN3NaHiTdb4Ju9CigldSisJyp4Cdfm9NERJWDcQvZNxsygmfnirQm8t7TI05m9gPfEnAzj06iIV%5Csir3MV8EPcIw49PwTVOk8fG56sM%3A1634195309083; WM_NI=QiOQMhd48gY02rYRhS6t7Hmt1LcePqUpGP7S2Zga8OH%2BePcjErnUxjdLZ0a7mCkJE%2FUnc7UIkm5Ryll8Qg9N9zwYrPXPziqwjafMSPDcrZOjdBanVK2BaG%2F3cgPiN1JQNm0%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee95b5488c8e96dabc40f3b88ea2d45a938b8ebaf46595abb78cc67487e7f7a8b32af0fea7c3b92a93b781b2d064a8ecf888dc3cf2ae97b3eb59f6acf9a9f080f394ad86d37b97b089b1b659f286a982bb458cf5abb1e26287aea2b6fb62a89496d1e43daf9da5abc82597b2add8d843a9b3fbb3fc68b0e89f88bc46fba6b9baf15c9c90b68af47daabfaaaed25c88bdbebbce62a89eaf8fd541f29eafa8ae39a1b9a08acd3b8a8f828eb337e2a3',
'referer': 'https://music.163.com/mv?id={}'.format(x),
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'same-origin',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1'
}
sessions = requests.session()
res = sessions.get(url=url,headers=headers).text
with open("mv.html",'w',encoding='utf-8')as fw:
fw.write(res)
import re
url1 = re.findall('<meta property="og:video" content=(.*>?)>',res)[0]
url2 = url1.split(' ')[0]
url3 = url2.replace('"','')
import urllib.parse as parse
# print(url3)
# 解析拿到的url
res_url = parse.unquote(url3)
print(res_url)
filepath = r"C:\Users\与久\PycharmProjects\爬虫\歌曲下载\{}.mp4".format(name)
# with open(filepath, 'w')as fw:
# fw.write(res_url)
url_1 =requests.get(res_url).content
f = open(filepath,'wb')
f.write(url_1)
f.close()
print("下载成功")
wy_music()