基于Python批量下载音乐可以分为以下几个步骤:
步骤一:确定下载源和下载目录
首先,要确认所需要下载的音乐源,比如 https://music.163.com/ 。然后,要确定下载的目录。
步骤二:获取音乐详情
通过分析音乐信息,可以获取歌曲ID,歌曲名称,歌手等信息。比如可以使用Python的requests和BeautifulSoup库对网页进行解析来获取音乐详情
import requests
from bs4 import BeautifulSoup
url = 'https://music.163.com/playlist?id=5211200197'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.content, 'html.parser')
music_lists = soup.find_all('ul', {'class': 'f-hide'})[0]
musics = music_lists.find_all('a')
for music in musics:
print(music['href'], music.text)
步骤三:下载音乐文件
获取从步骤二中解析出的音乐ID和下载目录,然后构建出音乐文件的下载链接,使用Python的requests库下载音乐文件并保存到指定目录中。下面是具体的代码片段:
def download_music_music163(_music_id, _path):
"""
:param _music_id: 歌曲id
:param _path: 保存路径
"""
music_src = f"https://music.163.com/song/media/outer/url?id={_music_id}.mp3"
try:
r = requests.get(music_src, stream=True)
content_size = int(r.headers['content-length'])
except Exception as e:
print('【错误】当前音乐无法下载')
return
if os.path.exists(_path):
if not check_file_already_exists(_path, content_size):
os.remove(_path)
else:
return
download_file_count = 0
while download_file_count < 3:
try:
with open(_path, "wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
download_size = os.path.getsize(_path)
if download_size >= content_size:
break
download_file_count += 1
except Exception as e:
download_file_count += 1
if download_file_count == 3:
print('【错误】这个音乐文件下载失败了...')
else:
print(_path, '下载成功')
示例说明
下面给出两个简单的示例,分别是下载流行歌曲榜(周榜)的前五首歌曲以及下载网易云音乐热门歌单中的音乐。
示例1:下载流行歌曲榜的前五首歌曲
import requests
from bs4 import BeautifulSoup
import os
def download_music_music163(_music_id, _path):
# 与前面的代码一致
pass
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/58.0.3029.110 Safari/537.3'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.content, 'html.parser')
music_lists = soup.find_all('ul', {'class': 'f-hide'})[0]
musics = music_lists.find_all('a')
top_n = 5 # 需要下载的歌曲数,可以自己设置
download_count = 0 # 已经下载的歌曲数
for music in musics:
if download_count == top_n:
break
music_id = music['href'].split('=')[1]
music_name = music.text
download_path = os.path.join(os.getcwd(), f'{music_name}.mp3')
download_music_music163(music_id, download_path)
download_count += 1
示例2:下载网易云音乐热门歌单中的音乐
import requests
from bs4 import BeautifulSoup
import os
def download_music_music163(_music_id, _path):
# 与前面的代码一致
pass
url = 'https://music.163.com/playlist?id=2884035'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.content, 'html.parser')
music_lists = soup.find_all('ul', {'class': 'f-hide'})[0]
musics = music_lists.find_all('a')
for music in musics:
music_id = music['href'].split('=')[1]
music_name = music.text
download_path = os.path.join(os.getcwd(), f'{music_name}.mp3')
download_music_music163(music_id, download_path)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何基于Python批量下载音乐 - Python技术站