本帖最后由 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"][i]["musicrid"]
key=key[6:]
self.key.append(key)
self.k=int(input("请输入需要下载歌曲的编号:"))
return self.key[self.k-1]
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[self.k-1]+".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()
|