python 根据网易云歌曲的ID 直接下载歌曲的实例
作者:行者刘6 时间:2021-01-15 19:48:47
特么的,上次写了一堆,发现,原来下载网易云的歌曲根本不用这么费劲,直接用!
http://music.163.com/song/media/outer/url?id=这里填歌曲id.mp3
这个URL就可以下载了,真特么 * !!
现在再来做一次!根据歌单下载歌曲
import requests,os,time,sys,re
from scrapy.selector import Selector
class wangyiyun():
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'Referer': 'http://music.163.com/'}
self.main_url='http://music.163.com/'
self.session = requests.Session()
self.session.headers=self.headers
def get_songurls(self,playlist):
'''进入所选歌单页面,得出歌单里每首歌各自的ID 形式就是“song?id=64006"'''
url=self.main_url+'playlist?id=%d'% playlist
re= self.session.get(url) #直接用session进入网页,懒得构造了
sel=Selector(text=re.text) #用scrapy的Selector,懒得用BS4了
songurls=sel.xpath('//ul[@class="f-hide"]/li/a/@href').extract()
return songurls #所有歌曲组成的list
##['/song?id=64006', '/song?id=63959', '/song?id=25642714', '/song?id=63914', '/song?id=4878122', '/song?id=63650']
def get_songinfo(self,songurl):
'''根据songid进入每首歌信息的网址,得到歌曲的信息
return:'64006','陈小春-失恋王'''
url=self.main_url+songurl
re=self.session.get(url)
sel=Selector(text=re.text)
song_id = url.split('=')[1]
song_name = sel.xpath("//em[@class='f-ff2']/text()").extract_first()
singer= '&'.join(sel.xpath("//p[@class='des s-fc4']/span/a/text()").extract())
songname=singer+'-'+song_name
return str(song_id),songname
def download_song(self, songurl, dir_path):
'''根据歌曲url,下载mp3文件'''
song_id, songname = self.get_songinfo(songurl) # 根据歌曲url得出ID、歌名
song_url = 'http://music.163.com/song/media/outer/url?id=%s.mp3'%song_id
path = dir_path + os.sep + songname + '.mp3' # 文件路径
requests.urlretrieve(song_url, path) # 下载文件
def work(self, playlist):
songurls = self.get_songurls(playlist) # 输入歌单编号,得到歌单所有歌曲的url
dir_path = r'C:\Users\Administrator\Desktop'
for songurl in songurls:
self.download_song(songurl, dir_path) # 下载歌曲
if __name__ == '__main__':
d = wangyiyun()
d.work(2214059025)
搞定!代码简单得一塌糊涂,,真特么费脑子!!
来源:https://blog.csdn.net/qq_38282706/article/details/80300546
标签:python,网易云,ID,下载歌曲
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP经常用到的函数
2009-07-06 13:00:00
关于Python中object类特殊方法的解释
2023-08-31 22:19:24
python如何发布自已pip项目的方法步骤
2023-01-22 01:17:36
理解和使用Oracle 8i分析工具LogMiner
2010-07-16 13:22:00
如何在django中运行scrapy框架
2021-01-16 21:26:54
python数据分析matplotlib的基础绘图使用
2022-09-08 18:07:08
![](https://img.aspxhome.com/file/2023/8/78548_0s.png)
Python random库使用方法及异常处理方案
2023-10-07 13:43:46
![](https://img.aspxhome.com/file/2023/1/82701_0s.jpg)
设计规范有谱么?
2008-06-06 12:28:00
Python+OpenCV实现将图像转换为二进制格式
2021-06-25 08:10:33
Python的子线程和子进程是如何手动结束的?
2022-09-08 18:54:41
一文带你搞懂Python中的数据容器
2021-05-25 16:09:59
三谈Iframe自适应高度
2010-08-03 13:04:00
System.Data.OleDb.OleDbDataAdapter与System.Data.OleDb.OleDbDataReader的区别是什么?
2009-10-29 12:17:00
Python中函数及默认参数的定义与调用操作实例分析
2022-01-15 05:16:31
![](https://img.aspxhome.com/file/2023/2/90302_0s.jpg)
网页设计进阶之六-- 守住那些不能丢的东西
2008-06-12 13:06:00
Python利用heapq实现一个优先级队列的方法
2021-08-05 06:28:48
![](https://img.aspxhome.com/file/2023/6/89446_0s.jpg)
js倒计时代码
2008-05-07 13:41:00
在Qt中正确的设置窗体的背景图片的几种方法总结
2023-05-11 11:15:01
如何避免查询调查结果时出现不相关主题的重复记录?
2009-11-07 18:42:00
利用Python如何生成便签图片详解
2021-01-02 18:09:52
![](https://img.aspxhome.com/file/2023/1/80441_0s.png)