爬取酷X音乐
本帖最后由 wushaominkk 于 2019-11-13 19:06 编辑这两天在学校学习了爬虫,闲来无事琢磨了关于酷我音乐爬取代码如下import requests
import os
class Kuwo(object):
def __init__(self):
self.key=[]
self.url=[]
self.musicname=[]
self.k=0
def getKey(self):
name=input("请输入搜索音乐的名字:")
heads = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1572856495,1572856564; Hm_lvt_eaa57ca47dacb4ad4f5a257001a3457c=1572856501,1572856570; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1572856670; Hm_lpvt_eaa57ca47dacb4ad4f5a257001a3457c=1572856676; kw_token=TA404V6U3ON',
'csrf': 'TA404V6U3ON',
'Host': 'www.kuwo.cn',
'Pragma': 'no-cache',
'Referer': 'http://www.kuwo.cn/search/list?key=%E5%8D%97%E6%96%B9%E5%A7%91%E5%A8%98',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'
}
url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key="+name+"&pn=1&rn=30"
htmldata = requests.get(url,headers=heads)
data=htmldata.json()
print(len(data["data"]["list"]))
for count,i in enumerate(data["data"]["list"]):
print(count+1,i['artist'],"--",i['name'])
self.musicname.append(i['artist']+"--"+i['name'])
for i in range(30):
key=data["data"]["list"]["musicrid"]
key=key
self.key.append(key)
self.k=int(input("请输入需要下载歌曲的编号:"))
return self.key
def getMusicUrl(self):
k=self.getKey()
url="http://www.kuwo.cn/url?format=mp3&rid="+k+"&type=convert_url3"
htmldata=requests.get(url)
data=htmldata.json()
url=data['url']
self.url=url
def Download(self):
mpath=os.getcwd()
mpath=os.path.join(mpath,"kuwomusic")
os.chdir(mpath)
music=requests.get(self.url)
music=music.content
f=open(self.musicname+".mp3",'wb')
f.write(music)
f.close()
print("下载完成!")
if __name__ == '__main__':
if os.path.exists("kuwomusic"):
pass
else:
os.mkdir("kuwomusic")
music=Kuwo()
music.getMusicUrl()
music.Download() 整理了一下,总算能用了!
import requests
import os
class Kuwo(object):
def __init__(self):
self.key = []
self.url = []
self.musicname = []
self.k = 0
def getKey(self):
name = input("请输入搜索音乐的名字:")
heads = {
'Accept': 'application/json, text/plain, /',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1572856495,1572856564; Hm_lvt_eaa57ca47dacb4ad4f5a257001a3457c=1572856501,1572856570; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1572856670; Hm_lpvt_eaa57ca47dacb4ad4f5a257001a3457c=1572856676; kw_token=TA404V6U3ON',
'csrf': 'TA404V6U3ON',
'Host': 'www.kuwo.cn',
'Pragma': 'no-cache',
'Referer': 'http://www.kuwo.cn/search/list?key=%E5%8D%97%E6%96%B9%E5%A7%91%E5%A8%98',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'
}
url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=" + name + "&pn=1&rn=30"
htmldata = requests.get(url, headers=heads)
data = htmldata.json()
print(len(data["data"]["list"]))
for count, i in enumerate(data["data"]["list"]):
print(count + 1, i['artist'], "--", i['name'])
self.musicname.append(i['artist'] + "--" + i['name'])
for i in range(30):
key = data["data"]["list"]["musicrid"]
key = key
self.key.append(key)
self.k = int(input("请输入需要下载歌曲的编号:"))
return self.key
def getMusicUrl(self):
k = self.getKey()
url = "http://www.kuwo.cn/url?format=mp3&rid=" + k + "&type=convert_url3"
htmldata = requests.get(url)
data = htmldata.json()
url = data['url']
self.url = url
def Download(self):
mpath = os.getcwd()
mpath = os.path.join(mpath, "kuwomusic")
os.chdir(mpath)
music = requests.get(self.url)
music = music.content
f = open(self.musicname + ".mp3", 'wb')
f.write(music)
f.close()
print("下载完成!")
if __name__ == '__main__':
if os.path.exists("kuwomusic"):
pass
else:
os.mkdir("kuwomusic")
music = Kuwo()
music.getMusicUrl()
music.Download() "for i in range(30): 这里最好写成range(count),否则如果歌曲数不够30,会报错
key = data["data"]["list"]["musicrid"]"
虽然没看懂,但支持楼主!! 没注释小白一枚,看起来有些地方不明白{:1_937:} 看了一下,是不是写死了一个列表,然后再根据这个列表下载 小白复制粘贴后,运行。。。咦,怎么这么多报错。。。。。{:1_926:} 还没测试你这代码呢,看代码好像可以下载音乐,是吗? 有些没有看懂,但还是支持楼主 支持楼主
支持楼主
页:
[1]
2