Python--自动下载音乐

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值