python语言音乐mp3爬虫程序代码
import requests
import re
import os
name = input(“请输入歌手名字:”)
print(“请输入要下载该歌手的歌曲数量(整数):”)
number = int(input())
url = ‘http://www.2t58.com/so/{}/1.html’.format(name)
url = ‘http://www.2t58.com/singer/{}/1.html’.format(name)
response = requests.get(url=url)
ex = ‘
musicIndex = re.findall(ex, response.text, re.S)
smallmusicList = []
for j in range(0, number):
smallmusicList.append(musicIndex[j])
print(smallmusicList)
headers = {
‘Accept’: ‘application/json, text/javascript, /; q=0.01’,
‘Accept-Encoding’: ‘gzip, deflate’,
‘Accept-Language’: ‘zh-CN,zh;q=0.9’,
‘Connection’: ‘keep-alive’,
‘Content-Length’: ‘26’,
‘Content-Type’: ‘application/x-www-form-urlencoded; charset=UTF-8’,
‘Cookie’: ‘Hm_lvt_b8f2e33447143b75e7e4463e224d6b7f=1690974946; Hm_lpvt_b8f2e33447143b75e7e4463e224d6b7f=1690976158’,
‘Host’: ‘www.2t58.com’,
‘Origin’: ‘http://www.2t58.com’,
‘Referer’: ‘http://www.2t58.com/song/bWhzc3hud25u.html’,
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36’,
‘X-Requested-With’: ‘XMLHttpRequest’
}
for i in smallmusicList:
data = {‘id’: i, ‘type’: ‘music’}
url2 = 'http://www.2t58.com/js/play.php'
response2 = requests.post(url=url2, headers=headers, data=data)
json_data = response2.json()
musicList = json_data['url']
musicResponse = requests.get(url=musicList)
filename = json_data['title'] + '.mp3'
if not os.path.exists('./music'):
os.mkdir('./music')
with open('./music/' + filename, 'wb') as f:
f.write(musicResponse.content)
print(filename + '下载成功!')
更多推荐
所有评论(0)