ps:个人歌单和网络共享歌单均可。
'''
@Author: ZoJet
@Date: 2019-08-09 14:14:10
@Description: 爬取网易云音乐歌单歌曲
'''
import os
import requests
import re
headers={
'Referer':'https://music.163.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36'
}
def download_songs(list_url):
res = requests.get(list_url, headers=headers)
list_title = re.findall(r'<title>(.*?)</title>',res.text)[0][:-13].replace('/', '_')
folder_path = "Download/Music163/"+list_title
if not os.path.exists(folder_path):
os.mkdir(folder_path)
song_count = 0
song_ids = re.findall(r'<a href="/song\?id=(\d+)">(.*?)</a>', res.text)
for song_id in song_ids:
download_url = "http://music.163.com/song/media/outer/url?id=%s" % song_id[0]
try:
with open(folder_path + "/" + song_id[1] +'.mp3', 'wb') as f:
song_count += 1
f.write(requests.get(download_url, headers=headers).content)
except FileNotFoundError:
print("%s 下载失败" % song_id[1])
pass
except OSError:
print("%s 下载失败" % song_id[1])
pass
print("总共%d首歌,下载完毕!" % song_count)
if __name__ == '__main__':
# 2019129958
list_id = input("请输入歌单id:")
list_url = 'https://music.163.com/playlist?id=%s' % list_id
download_songs(list_url)