下载好听轻音乐爬虫,亲测有效,仅供学习~
// An highlighted block
import requests
import re
import time
songid=[]
songname=[]
header={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",\
"Referer":"http://www.htqyy.com/top/hot"}
#获取榜单url
#http://www.htqyy.com/top/musicList/hot?pageIndex=0&pageSize=2
#播放歌曲URL
#http://www.htqyy.com/play/609
#音乐链接
# http://f2.htqyy.com/play8/33/mp3/6
# http://f2.htqyy.com/play8/62/mp3/6
#resp=requests.get("http://www.htqyy.com/top/musicList/hot?pageIndex=0&pageSize=20",headers=header)
#print(resp.text)
page=int(input("我要下载多少页音乐"))
for i in range(0,page):
resp=requests.get("http://www.htqyy.com/top/musicList/hot?pageIndex="+str(i)+"&pageSize=20",headers=header).text
patten1='sid="(.*?)"'
patten2='title="(.*?)" sid'
idlist=re.findall(patten1,resp)
namelist=re.findall(patten2,resp)
songid.extend(idlist)
songname.extend(namelist)
for i in range(len(songid)):
print("正在下载第",i+1,"首歌,","歌名:",songname[i])
url="http://f2.htqyy.com/play8/"+str(songid[i])+"/mp3/6"
data=requests.get(url).content
with open("E:\\music\\{}.mp3".format(songname[i]),"wb") as f:
f.write(data)
time.sleep(5)
print("共下载",len(songid),"首歌,完成~")