import re
import os
import requests
headers = {
'User-Agent': 'Chrome/10'
}
def creat_file(filename):
if not os.path.exists(filename):
os.mkdir(filename)
os.chdir(filename)
def main():
print('许嵩的ID:5771')
ID = input('请输入你喜欢的歌手的ID:')
url = 'https://music.163.com/artist?id=' + ID
html_text = requests.get(url, headers).text
creat_singer(html_text)
#创建以歌手命名的文件夹保存下载的音乐
def creat_singer(html_text):
findname = re.compile(r'<h2 id="artist-name" data-rid=\d* class="sname f-thide sname-max" title=".*?">(.*?)</h2>')
name = re.findall(findname, html_text)[0]
print(name)
creat_file(name)
find_id(html_text)
#寻找歌曲id,和歌曲名,并下载
def find_id(html_text):
html_data = re.findall('<li><a href="/song\?id=(\d+)">(.*?)</a></li>', html_text)
for song_id, song_name in html_data:
music_id = f'http://music.163.com/song/media/outer/url?id={
song_id}.mp3'
music_content = requests.get(url=music_id, headers=headers).content
with open(song_name + '.m4a', 'wb') as f:
f.write(music_content)
print(song_id, song_name, '下载完毕!')
main()
csdn的审核是人审吗?S-two审核