今天我们用Python来写一个简单的酷狗下载程序
我将这个程序打包成exe文件,有兴趣的可以看下。
链接:https://pan.baidu.com/s/1K2ETIL09-Xwpe4pBtojNiQ
提取码:1b1o
我们先上代码,看下步骤:
import requests
import json
import os
import urllib.parse
#j为歌曲标头
j=1
class KuGou:
#初始化
def __init__(self,word):
#创建目录
path="F:/酷狗歌曲下载/" #路径可以自己定义
if not os.path.exists(path):
os.mkdir(path)
self.path=path
self.word=word
word1= urllib.parse.quote(word)
#带参数访问网址
self.url="https://songsearch.kugou.com/song_search_v2?callback=jQuery112404383246257004936_1550576119140&keyword={}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1550576119142".format(word1)
#发出requests请求
def requests_get(self):
r=requests.get(self.url)
r.encoding="utf-8"
self.req = r.text
# print(self.req)
#获取并且展示歌曲信息
def show_song(self):
global j
# songID_list={}
songhash_list={} #hash列表
song_name={} #歌曲名字
# json文件信息
kg=json.loads(self.req.lstrip("jQuery112404383246257004936_1550576119140(").rstrip().rstrip(r')'))
self.kg=kg
for song in kg['data']['lists']:
print("{0}————{1}".format(j,song['SongName']))
song_name[str(j)]=song['SongName']
songhash_list[str(j)]=song['FileHash']
j+=1
self.song_name=song_name
self.songhash_list=songhash_list
# self.songID_list=songID_list
# print(songhash_list)
#下载歌曲
def download_song(self):
num=input("您想下载哪首歌(输数字):")
url="http://www.kugou.com/yy/index.php?r=play/getdata&hash={}".format(self.songhash_list[num])
hash_res=requests.get(url)
hash_js=hash_res.json()
play_url=hash_js['data']['play_url']
# print(play_url)
with open(self.path+self.song_name[num]+".mp3","wb") as f:
f.write(requests.get(play_url).content)
print("《{}》下载完成".format(self.song_name[num]))
#选择是否继续爬取
def exit(self):
str1=input("继续下载(y),还是退出(n):")
return str1
if __name__ == "__main__":
print("欢迎来到酷狗下载系统")
word=input("请输入歌手名:")
# word="杨宗纬"
pachong=KuGou(word)
pachong.requests_get()
pachong.show_song()
pachong.download_song()
str1=pachong.exit()
while str1!="n":
if str1 == "y":
pachong.download_song()
str1 = pachong.exit()
elif str1 == "n":
print("已退出")
else:
print("输入错误,请重新输入!")
str1 = pachong.exit()
import requests
import json
import os
import urllib.parse
#j为歌曲标头
j=1
class KuGou:
#初始化
def __init__(self,word):
#创建目录
path="F:/酷狗歌曲下载/" #路径可以自己定义
if not os.path.exists(path):
os.mkdir(path)
self.path=path
self.word=word
word1= urllib.parse.quote(word)
#带参数访问网址
self.url="https://songsearch.kugou.com/song_search_v2?callback=jQuery112404383246257004936_1550576119140&keyword={}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1550576119142".format(word1)
我们来解析下酷狗的网址:
https://songsearch.kugou.com/song_search_v2?callback=jQuery112404383246257004936_1550576119140&keyword= {“这是关键字”}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1550576119142
#发出requests请求
def requests_get(self):
r=requests.get(self.url)
r.encoding="utf-8"
self.req = r.text
# print(self.req)
#获取并且展示歌曲信息
def show_song(self):
global j
# songID_list={}
songhash_list={} #hash列表
song_name={} #歌曲名字
# json文件信息
kg=json.loads(self.req.lstrip("jQuery112404383246257004936_1550576119140(").rstrip().rstrip(r')'))
self.kg=kg
for song in kg['data']['lists']:
print("{0}————{1}".format(j,song['SongName']))
song_name[str(j)]=song['SongName']
songhash_list[str(j)]=song['FileHash']
j+=1
self.song_name=song_name
self.songhash_list=songhash_list
# self.songID_list=songID_list
# print(songhash_list)
song_name是一个字典,存放歌曲名字,格式:{‘第几首’:“歌曲名字”}
songhash_list是一个字典,存放歌曲链接的hash参数(很重要)
#下载歌曲
def download_song(self):
num=input("您想下载哪首歌(输数字):")
url="http://www.kugou.com/yy/index.php?r=play/getdata&hash={}".format(self.songhash_list[num])
hash_res=requests.get(url)
hash_js=hash_res.json()
play_url=hash_js['data']['play_url']
# print(play_url)
with open(self.path+self.song_name[num]+".mp3","wb") as f:
f.write(requests.get(play_url).content)
print("《{}》下载完成".format(self.song_name[num]))
#====================================================================================================
#选择是否继续爬取
def exit(self):
str1=input("继续下载(y),还是退出(n):")
return str1
if __name__ == "__main__":
print("欢迎来到酷狗下载系统")
word=input("请输入歌手名:")
# word="杨宗纬"
pachong=KuGou(word)
pachong.requests_get()
pachong.show_song()
pachong.download_song()
str1=pachong.exit()
while str1!="n":
if str1 == "y":
pachong.download_song()
str1 = pachong.exit()
elif str1 == "n":
print("已退出")
else:
print("输入错误,请重新输入!")
str1 = pachong.exit()
您的支持,是我前进最大的动力!