import re
import time
import requests
import json
# 榜单第一页地址 http://www.htqyy.com/artist/musicList/1?pageIndex=0&pageSize=20&order=hot
# 榜单第二页地址 http://www.htqyy.com/artist/musicList/1?pageIndex=1&pageSize=20&order=hot
# 榜单第三页地址 http://www.htqyy.com/artist/musicList/1?pageIndex=2&pageSize=20&order=hot
# 歌曲url http://www.htqyy.com/play/33
# 歌曲资源所在地址: http://f3.htqyy.com/play9/33/mp3/3
header11 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
songName = []
songId = []
page = int(input("请输入你要爬取的页数:"))
for i in range(0, page):
url = "http://www.htqyy.com/artist/musicList/1?pageIndex="+ str() +"&pageSize=20&order=hot"
# 获取音乐页面信息
html = requests.get(url, headers=header11)
strr = html.text
# print(html.text)
pat1 = r'title="(.*?)" sid'
pat2 = r'sid="(.*?)"'
titleList = re.findall(pat1, strr)
idList = re.findall(pat2, strr)
songId.extend(idList)
songName.extend(titleList)
# print(songId)
# print(songName)
# print(len(songId))
# print(len(songName))
for i in range(0,len(songId)):
songUrl = "http://f3.htqyy.com/play9/"+str(songId[i])+"/mp3/3"
songname = songName[i]
date = requests.get(songUrl, headers=header11).content
print ("正在下载第"+str(i)+"首歌,名字为:"+songName[i])
with open("E:\\music\\{}.mp3".format(songname), "wb") as f:
f.write(date)
# 歌曲主要信息 <span class="title"><a href="/play/33" target="play" title="清晨" sid="33">清晨</a></span>
Python--自动下载音乐
最新推荐文章于 2024-07-23 15:45:06 发布