import re
import requests
import os
import time
# 根据歌手的网易云ID爬取其网易云上的歌曲
headers = {
'User-Agent': 'Chrome/10'
}
def main():
print('例如:华晨宇的网址是:https://music.163.com/#/artist?id=861777', '\n', 'ID就是:861777。')
ID = input("请输入歌手ID:")
url = 'https://music.163.com/artist?id=' + ID
html = requests.get(url, headers).text
get_id(html)
def get_id(html):
finding = re.compile(r'<a href="/song\?id=(\d*)">(.*?)</a></li><li>')
filename = re.compile(r'<h2 id="artist-name" data-rid=\d* class="sname f-thide sname-max" title=".*?">(.*?)</h2>')
singers = re.findall(filename, html)[0]
creat(singers)
ll = re.findall(finding, html)
for i in ll:
savemusic(i[1], i[0])
time.sleep(0.5)
def creat(singers): # 创建文件夹
if not os.path.exists(singers):
os.mkdir(singers) # 如果该目录不存在就创建它
os.chdir(singers)
def savemusic(name, id): # 保存文件
url = 'https://music.163.com/song/media/outer/url?id=' + id + '.mp3'
with open(name + '.m4a', 'wb') as f:
print('歌曲《', name, '》 下载中***************')
f.write(requests.get(url=url, headers=headers).content)
f.close()
print("《", name, "》下载完成")
print('')
if __name__ == '__main__':
main()
Python爬虫练习
最新推荐文章于 2024-03-10 19:25:26 发布