思路
获取所有歌曲id 和名字
使用网易云下载接口进行拼接
http://music.163.com/song/media/outer/url?id=483937795
代码
import requests
import re
import os
'''
http://music.163.com/song/media/outer/url?id=483937795
'''
file_name = '网易云\\'
if not os.path.exists(file_name):
os.mkdir(file_name)
def get_song():
url = 'https://music.163.com/discover/toplist?id=3779629'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
response = requests.get(url=url, headers=headers).text
# print(response)
lis = re.findall('href="/song\\?id=(.*?)">(.*?)</a></li><li><a', response)
print(lis)
return lis
songs = get_song()
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
for li in songs:
title = li[1]
title = re.sub(r'[\\/:"<>|*?]', '', title)
music_url = 'http://music.163.com/song/media/outer/url?id=' + li[0]
print(music_url, li[1])
content = requests.get(url=music_url, headers=headers).content
with open(file_name + title + '.mp3', mode='wb') as f:
f.write(content)