Python版本3.9
目錄下需要'下載音樂(lè)'文件夾
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 創(chuàng)建時(shí)間 : 2021/4/26 20:12
# 創(chuàng)建人 : 空城舊念
# 文件 : 下載音樂(lè).py
# 平臺(tái): PyCharm
import requests
import jsonpath
from urllib.request import urlretrieve
import urllib.parse
def get_csrf(keyword, s):
header = {
'Accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, image / apng, '
'* / *;q = 0.8, application / signed - exchange;v = b3;q = 0.9',
'Accept - Encoding': 'gzip, deflate',
'Accept - Language': 'zh - CN, zh;q = 0.9',
'Cache - Control': 'max - age = 0',
'Connection': 'keep - alive',
# 'Cookie': '_ga = GA1.2.1157256760.1619264212;_gid =
# GA1.2.572513955.1619264212;Hm_lvt_cdb524f42f0ce19b169a8071123a4797 = 1619264212, 1619264427,
# 1619267361;_gat = 1;Hm_pvt_cdb524f42f0ce19b169a8071123a4797 = 1619269262;kw_token = 5ASAP1Y703',
'Host': 'www.kuwo.cn',
'If - None - Match': '"34446-xe3aVUY1idCm93W9EK4AU7RIIkk"',
'Upgrade - Insecure - Requests': '1',
'User - Agent': 'Mozilla / 5.0(WindowsNT10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / '
'84.0.4147.89Safari / 537.36SLBrowser / 7.0.0.2261SLBChan / 30 '
}
url = "http://www.kuwo.cn/"
s.get(url, headers=header)
# print("kw_token" + "-" * 10)
# print(s.cookies.get_dict())
# print("kw_token"+"-"*10)
heads = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
# 'Cookie': '_ga=GA1.2.1157256760.1619264212; _gid=GA1.2.572513955.1619264212;
# Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1619264212,1619264427,1619267361;
# Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1619267443; kw_token=J9LYV05NY',
'csrf': s.cookies.get_dict()['kw_token'],
'Host': 'www.kuwo.cn',
'Referer': f'http://www.kuwo.cn/search/list?key={keyword}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.2261 SLBChan/30 '
}
# print("請(qǐng)求頭"+"-"*10)
# print(heads)
# print("請(qǐng)求頭" + "-" * 10)
urls = f"http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={keyword}&pn=1&rn=30&httpsStatus=1&reqId" \
f"=ec84e110-a4fb-11eb-a1fc-5fd89c65d88b "
# html = requests.get(url, headers=head).text
r = s.get(urls, headers=heads)
# print(r.text)
rids = jsonpath.jsonpath(r.json(), '$..rid')[0]
# print(rids)
return rids
def get_music_url(rids, keyword, s):
url = f'http://www.kuwo.cn/url?format=mp3&rid={rids}&response=url&type=convert_url3&br=128kmp3&from=web&t' \
f'=1619270497710&httpsStatus=1 '
head = {
'Accept': 'application / json, text / plain, * / *',
'Accept - Encoding': 'gzip, deflate',
'Accept - Language': 'zh - CN, zh;q = 0.9',
'Connection': 'keep - alive',
# 'Cookie': '_ga = GA1.2.1157256760.1619264212;_gid =
# GA1.2.572513955.1619264212;Hm_lvt_cdb524f42f0ce19b169a8071123a4797 = 1619264212, 1619264427,
# 1619267361;Hm_lpvt_cdb524f42f0ce19b169a8071123a4797 = 1619269271;_gat = 1;kw_token = WMZ5EQS5F9G',
'Host': 'www.kuwo.cn',
'Referer': f'http: // www.kuwo.cn / search / list?key = {keyword}',
'User - Agent': 'Mozilla / 5.0(WindowsNT10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / '
'84.0.4147.89Safari / 537.36SLBrowser / 7.0.0.2261SLBChan / 30 '
}
# print(s.get(url, headers=head).text)
music_url = s.get(url, headers=head).json()['url']
return music_url
def get_music(music_url, keyword):
file = '下載音樂(lè)/' + urllib.parse.unquote(keyword) + '.mp3'
urlretrieve(music_url, file)
print("下載完成")
# 調(diào)用rid獲取, url解析, 音樂(lè)下載, 模塊
def run(keyword, s):
try:
rid = get_csrf(keyword, s) # rid獲取
music_url = get_music_url(rid, keyword, s) # url獲取
get_music(music_url, keyword) # 下載音樂(lè)
except Exception as re:
print(re)
print("查詢失敗")
return "查詢失敗了"
# 定義主函數(shù)
# if __name__ == "__main__":
def download(keyword):
s = requests.session()
# print("rid:"+s.cookies.get_dict()['kw_token'])
# print("rid:" + s.cookies.get_dict())
# keyword = input("請(qǐng)輸入你需要下載的音樂(lè):")
keyword = urllib.parse.quote(keyword)
pd = run(keyword, s)
if pd == "查詢失敗了":
return "查詢失敗了呀"
song = input("歌名:")
download(song)