Python 爬歌曲
Python 爬歌曲
小练习
import re
import time
import requests
# http://www.htqyy,com/top/hot
# http://f2.htqyy.com/play7/33/mp3/4
page=int(input("请输入要爬取的页数:"))
songID=[]
songName=[]
for i in range(0,page):
url="http://www.htqyy.com/top/musicList/hot?pageIndex= "+str(i)+"&pageSize=20"
html=requests.get(url)
strr=html.text
pat1=r'title="(.*?)" sid'
pat2=r'sid="(.*?)"'
idlist=re.findall(pat2,strr)
title=re.findall(pat1,strr)
print(title)
songID.extend(idlist)
songName.extend(title)
for i in range(0,len(songID)):
songurl="http://f2.htqyy.com/play7/"+str(songID[i])+"/mp3/4"
songname=songName[i]
data=requests.get(songurl).content
with open("D:\\music\\{}.mp3".format(songname),"wb") as f:
f.write(data)
print("正在下载第:",i+1,"首","歌曲名为:",songname)
time.sleep(0.5)
print(len(songID))
print(len(songName))
个性签名:独学而无友,则孤陋而寡闻!
如果觉得这篇文章对你有小小的帮助的话,记得点个“关注”哦,博主在此感谢!还可以扫码添加好友,交流编程上的问题哦!
万水千山总是情,点赞再走行不行!哈哈哈(っ•̀ω•́)っ✎⁾⁾!