[Python] 纯文本查看 复制代码
import os
import hashlib
import requests
import time
class KuGoumusic():
def __init__(self):
self.headers2 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
}
self.headers = {
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Accept': '*/*',
'Referer': 'https://www.kugou.com/yy/html/search.html',
'Connection': 'keep-alive',
'TE' : 'Trailers',
'Host' : 'complexsearch.kugou.com',
'cookie' : 'kg_mid=61fe1880c589517a9747d456c1c78519; kg_dfid=0hoVrE4EHqxb0nNjS13230j2; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; kg_mid_temp=61fe1880c589517a9747d456c1c78519'
} # 请求头
self.song = []
self.singer = []
self.FileHash = []
self.SQFileHash = []
self.HQFileHash = []
self.Duration = []
self.ct = int(time.time()*1000)
def get_signature(self,keyword):
o = {
0: "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
1: "bitrate=0",
3: "clienttime={0}".format(self.ct),
4: "clientver=2000",
5: "dfid=-",
6: "inputtype=0",
7: "iscorrection=1",
8: "isfuzzy=0",
9: "keyword={}".format(keyword),
10: "mid={0}".format(self.ct),
11: "page=1",
12: "pagesize=30",
13: "platform=WebFilter",
14: "privilege_filter=0",
15: "srcappid=2919",
16: "tag=em",
17: "userid=-1",
18: "uuid={0}".format(self.ct),
19: "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
}
date = ''
for li in o.values():
date = date + li
md5 = hashlib.md5()
md5.update(date.encode('utf-8'))
return md5.hexdigest()
def search(self):
keyword = input("请输入想要听的歌:")
url_0 = 'https://complexsearch.kugou.com/v2/search/song?&keyword={0}&page=1&pagesize=30&bitrate=0&isfuzzy=0&tag=em&inputtype=0&platform=WebFilter&userid=-1&clientver=2000&iscorrection=1&privilege_filter=0&srcappid=2919&clienttime={1}&mid={1}&uuid={1}&dfid=-&signature={2}'.format(keyword,self.ct,self.get_signature(keyword))
response = requests.get(url_0,headers=self.headers)
dict_0 = response.json()
music_list = dict_0['data']['lists']
for music in music_list:
song = music['SongName']
song = song.replace('<em>','').replace('</em>','')
singer = music['SingerName']
FileHash = music['FileHash']
SQFileHash = music['SQFileHash']
HQFileHash = music['HQFileHash']
Duration = music['Duration']
self.song.append(song)
self.singer.append(singer)
self.FileHash.append(FileHash)
self.SQFileHash.append(SQFileHash)
self.HQFileHash.append(HQFileHash)
self.Duration.append(Duration)
self.select()
def select(self):
ip = 0
for song, singer, FileHash, SQFileHash, HQFileHash, Duration in zip(self.song, self.singer, self.FileHash,
self.SQFileHash, self.HQFileHash,
self.Duration):
min = int(int(Duration)/60)
seconds = int(Duration)%60
time = str(min)+":"+str(seconds)
print(ip,song,singer,time)
ip +=1
ip = input("请输入序号:")
ip = int(ip)
song = self.song[ip]
singer = self.singer[ip]
FileHash = self.FileHash[ip]
self.download(song,singer,FileHash)
def download(self,song,singer,FileHash):
headers = {
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Accept': '*/*',
'Referer': 'https://www.kugou.com/',
'cookie': 'kg_mid=d2d37972966d97ed632044391f136e5b; kg_dfid=2mBPAo3OCSYx0tOVPh3CkjnP; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; kg_mid_temp=d2d37972966d97ed632044391f136e5b'
} # 请求头
url = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={0}'.format(
FileHash)
response = requests.get(url, headers=headers)
dict = response.json()
url = dict['data']['play_url']
music = requests.get(url, headers=self.headers2).content
dir = os.getcwd()
dir = os.path.join(dir, "酷狗音乐 ")
if not os.path.exists(dir):
os.mkdir(dir) # 构造文件夹
os.chdir(dir) # 将下载的歌曲存储在该文件夹
print(song, singer)
file_name = song + '-' + singer + '.mp3' # 文件名
with open(file_name, 'wb') as f:
f.write(music)
print("下载成功!")
if __name__ =='__main__':
music = KuGoumusic()
music.search()