下载好听轻音乐爬虫--爬虫学习

下载好听轻音乐爬虫,亲测有效,仅供学习~

// 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),"首歌,完成~")


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值