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))
posted @ 2019-05-17 16:50  『侠客行』  阅读(531)  评论(0编辑  收藏  举报