网易云歌单批量下载

借助python

import os
import requests
from bs4 import BeautifulSoup

def music_download(play_url):
    headers = {
        'User-Agent': (
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
            '(KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
        )
    }

    if not os.path.exists('./网易云歌单/'):
        os.mkdir('./网易云歌单/')

    s = requests.session()
    response = s.get(play_url, headers=headers).content
    soup = BeautifulSoup(response, 'html.parser')
    music_data = soup.find('ul', class_='f-hide')

    # 存储歌曲信息
    lists = []
    for music in music_data.find_all('a'):
        music_url = 'http://music.163.com/song/media/outer/url' + music['href'][5:] + '.mp3'
        music_name = music.text
        lists.append([music_name, music_url])
        print(f"发现歌曲:{music_name} -> {music_url}")

    os.chdir('./网易云歌单')
    print('当前目录变为为:', os.getcwd())

    for i in lists:
        url = i[1]
        name = i[0]
        try:
            print(f"正在下载: {name}")
            res = requests.get(url, headers=headers)
            with open(f'{name}.mp3', "ab") as f:
                f.write(res.content)
            print(f"下载成功: {name}")
        except Exception as e:
            print(f"下载失败: {name}, 错误: {e}")

if __name__ == "__main__":
    # 批量下载网易云歌单mp3(普通音质),这里我只是下载下来用于和其他地方下载的flac做特征匹配
    play_url = "https://music.163.com/album?id=71747233"
    music_download(play_url)
Last modification:November 30, 2024
V50%看看实力