连续访问会需要输入验证码,待完善:
import requests
import math,os
class Song(dict):
url="https://wwwapi.kugou.com/yy/index.php"
downloadParams={
"r":"play/getdata",
"hash":"",
"mid":"45fd4cb240e758115fb4ebcc265e4918",
}
def download(self,file:str):
self.downloadParams['hash']=self['FileHash']
data=requests.get(self.url,self.downloadParams,timeout=5).json()['data']
if(data['play_url']):
music=requests.get(data['play_url'],timeout=3)
with open(file+data['song_name'].replace('*','')+' - '+data['author_name']+'.'+self['ExtName'],"wb") as f:
f.write(music.content)
f.close()
class Kugou:
searchUrl="https://songsearch.kugou.com/song_search_v2"
searchParams={
"keyword": "",
"page": "1",
"pagesize": "30",
"platform": "WebFilter",
}
searchTotalPage=1 #总页数
downloadFile='' #下载存储路径
def __init__(self,file:str): #通过一个参数【下载路径】进行构造
self.downloadFile=file
if(not os.path.exists(file)):
os.makedirs(file)
def search(self,key:str): #根据关键字key获取所有搜索到的歌曲信息
songList=[]
first=self.__searchByPage(key,1)['data'] #通过第一页获取总数
songList.extend( Song(i) for i in first['lists'])
self.searchTotalPage=math.ceil(first['total']/30)
for page in range(2,self.searchTotalPage):
songList.extend(Song(i) for i in self.__searchByPage(key,page)['data']['lists'])
return songList
def __searchByPage(self,key:str,index:int): #根据关键字和页码获取三十条歌曲信息
self.searchParams['page']=str(index)
self.searchParams['keyword']=key
req=requests.get(self.searchUrl,self.searchParams,timeout=5).json()
return req
def getMusic(self,song:dict): #根据歌曲信息下载歌曲
url="https://wwwapi.kugou.com/yy/index.php"
self.downloadParams['hash']=song['FileHash']
data=requests.get(url,self.downloadParams).json()['data']
music=requests.get(data['play_url'])
with open(self.downloadFile+data['song_name']+' - '+data['author_name']+'.'+song['ExtName'],"wb") as f:
f.write(music.content)
f.close()
kugou=Kugou("d:/myMusic/")
songs=kugou.search("恋と愛")
for i in range(len(songs)): #通过关键字【恋と愛】返回相关歌曲列表:调试监视是爬虫必不可少的技能 *.-
print(i,songs[i]['FileName'])
songs[0].download(kugou.downloadFile) #下载第一首