Python3批量下载网易云音乐热歌榜

Python3批量下载网易云音乐热歌榜

可以批量下载网易云音乐热歌榜的歌曲,可以自己设定数量,速度非常快。

https://music.163.com/#/discover/toplist?id=3778678

需先安装以下两个模块

import requests
from bs4 import BeautifulSoup

首先,找到你要下载的歌曲,用网页版打开,复制链接中的歌曲ID,如:https://music.163.com/#/song?id=1428598981

这个链接ID就是 1428598981

然后将ID替换到链接http://music.163.com/song/media/outer/url?id=ID.mp3 中的ID位置即可获得歌曲的外链:

http://music.163.com/song/media/outer/url?id=1428598981.mp3

 

附源码:

 

来源,知乎Tsing的回答

欢迎关注+点赞+评论+收藏!

#-*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup

header = {    # 伪造浏览器头部,不然获取不到网易云音乐的页面源代码。
        'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
        'Referer':'http://93.174.95.27',
        }
link = "https://music.163.com/playlist?id=4880937603"
# 这是网易云音乐歌单的链接,注意删除链接中的'#'
#(其实是嵌套在网页里面含有歌曲数据的页面框架的真实链接)
r = requests.get(link, headers=header)
html = r.content
soup = BeautifulSoup(html, "html.parser")
songs = soup.find("ul", class_="f-hide").select("a", limit=10)
# 通过分析网页源代码发现排行榜中的歌曲信息全部放在类名称为 f-hide 的 ul 中
# 于是根据特殊的类名称查找相应 ul,然后找到里面的全部 a 标签
# 限制数量为 10,即歌单的前 10 首歌

i = 1
for s in songs:
    song_id = s['href'][9:]
    song_name = s.text
    song_down_link = "http://music.163.com/song/media/outer/url?id=" + song_id + ".mp3"
    print("第 " + str(i) + " 首歌曲:" + song_down_link)
    print("正在下载...")
    
    response = requests.get(song_down_link, headers=header).content
    f = open(song_name + ".mp3", 'wb')
    f.write(response)
    f.close()
    print("下载完成!\n\r")
    i = i+1

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值