import requests
import re
import os
file_name = 'music\\'
if not os.path.exists(file_name): # 如果没有这个文件
os.mkdir(file_name)
url = 'https://music.163.com/discover/toplist?id=3778678'
headers = {
'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'
}
res = requests.get(url=url, headers=headers)
html_data = re.findall(
'<li><a href="/song\?id=(\d+)">(.*?)</a></li>', res.text)
print(html_data)
for id, title in html_data:
music_url = f'https://music.163.com/song?id={id}.mp3'
# 获取二进制数据内容
music_content = requests.get(music_url, headers=headers).content
with open('music\\'+title+'.mp3', mode='wb') as f:
f.write(music_content)
Python实操网易云音乐爬虫实战-自动下载所有音乐
最新推荐文章于 2024-08-28 10:44:00 发布