python网易云音乐

python网易云音乐

管理员
2024-08-16 / 0 评论 / 23 阅读 / 正在检测是否收录...

    import requests
    from lxml import etree
    
    
    base_url = 'http://music.163.com/song/media/outer/url?id='
    url = 'https://music.163.com/discover/toplist'
    # url = 'https://music.163.com/playlist?id=2067685777'
    
    
    
    headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0."
        }
    request = requests.get(url,headers=headers).text
    html = etree.HTML(request)
    song_id = html.xpath('//a[contains(@href, "song?id")]/@href')
    song_names = html.xpath('//a[contains(@href, "song?id")]/text()')
    
    for song_id,song_name in zip(song_id,song_names) :
        song_id = song_id.strip('/song?id=')
        if ('$'in song_id) == False:
            print(song_name)
            mp3_url = base_url + song_id
            mp3 = requests.get(mp3_url,headers=headers).content
    
            with open(f'./网易云音乐/{song_name }.mp3','wb') as file :
                file.write(mp3)
0

评论 (0)

取消