还在用网易云音乐客户端?out了
<h1 style="color: black; text-align: left; margin-bottom: 10px;">前言</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网易云音乐随着互联网时代的来临,优秀的以“大数据”而闻名。自2016年以后,越来越多人接受了这款优秀的音乐网站 or app,因良好的用户体验感而出名,<span style="color: black;">例如</span>歌单这个功能,<span style="color: black;">仅有</span>你想不到的,<span style="color: black;">无</span>用户共享、上传歌单做不到的!什么张雨生合集呀,什么动漫名曲。。。等</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>歌单这项功能<span style="color: black;">来讲</span>,应该是许多网易云用户最<span style="color: black;">喜欢</span>的功能之一,毕竟整合了许多符合自己口味的歌曲在<span style="color: black;">一块</span>,省时又省力。但<span style="color: black;">亦</span><span style="color: black;">同期</span>面临着一个问题,即下载!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假如你的电脑上装了网易云音乐的客户端,<span style="color: black;">那样</span>还好说,<span style="color: black;">例如</span>下面<span style="color: black;">这般</span>,你<span style="color: black;">能够</span>一键下载:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/d89221ae136449ac9b70be2d51cb640a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=8SR7tYKLeETyLQdsKdXE%2FIB%2Bb4Y%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然,<span style="color: black;">亦</span>不排除有些<span style="color: black;">伴侣</span>不<span style="color: black;">爱好</span>这么麻烦,还要装个客户端。。。<span style="color: black;">例如</span>我,能不装电脑上的软件,绝对不会装!<span style="color: black;">因此</span>浏览器听歌<span style="color: black;">成为了</span>首选,局限性<span style="color: black;">便是</span><span style="color: black;">倘若</span><span style="color: black;">无</span>网,<span style="color: black;">那样</span>浏览器这条路就走不通了!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>今天,咱们就用<strong style="color: blue;">爬虫</strong>的方式来实现一下,<span style="color: black;">怎样</span>用 Python 下载歌单下的所有歌曲!顺便<span style="color: black;">认识</span>下爬虫下载音乐的原理机制。<span style="color: black;">亦</span>许下载网易云音乐的<span style="color: black;">文案</span>满大街都是,但<span style="color: black;">详细</span>实现思路,笔者觉得应该一人一个样,不妨耐心的看下去,说<span style="color: black;">欠好</span>有意外收获!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">PS:本文目的是抱以学习<span style="color: black;">心理</span>而分享,禁止用于<span style="color: black;">违法</span>以及<span style="color: black;">商场</span>途径,如有<span style="color: black;">危害</span>,一切后果自己承担!</strong></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">成果演示</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">依然是先看成果,有了成果,<span style="color: black;">才可</span>勾起你的兴趣,看了成果还提不起兴趣的,那确实<span style="color: black;">便是</span>没兴趣了,<span style="color: black;">亦</span><span style="color: black;">不消</span>在往下看了!走着客官,上图~</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/8dbbf3422cf84f8eb7623c6325d3e679~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=752YUWry%2FUq84YGY%2BMNmZvmSUMs%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/c6a10122990a4731bcf186b131e2a2dd~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=Cr5A5hghuD%2F36G%2Fx%2BsMkuOP5x90%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">爬取以及设计思路</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1. 找下载歌曲的外链(对外链接,简<span style="color: black;">叫作</span>外链)<span style="color: black;">位置</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">既然<span style="color: black;">重点</span>功能是下载,<span style="color: black;">那样</span>肯定是需要找到对应下载音乐的<span style="color: black;">位置</span>的,<span style="color: black;">倘若</span>单从网易云主页分析,还要破解 js 加密什么的,实在麻烦,不妨换一种思路!多多利用搜索引擎,经<span style="color: black;">查找</span>,<span style="color: black;">发掘</span>网易云<span style="color: black;">供给</span>了一个对外下载歌曲的<span style="color: black;">位置</span>,只需要<span style="color: black;">晓得</span>歌曲id,<span style="color: black;">就可</span><span style="color: black;">运用</span><span style="color: black;">位置</span>加id形式打开网页进行播放,<span style="color: black;">位置</span>如下:</p># 只需要将歌曲 id 传给此网页,直接打开<span style="color: black;">就可</span>播放!
http://music.163.com/song/media/outer/url?id=xxxxx.mp3<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">来试试,"金鱼邓紫棋"的这首歌,翻唱龙卷风,id=28427775:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/6650738554bb45069eaeace026497362~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=CzA5Vw47jcvUIEhhiBpphA3fSF4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">把id替换到上面给出的外链中,打开后,网址自动<span style="color: black;">转</span>,将url加密了,<span style="color: black;">同期</span>自动播放:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/24dfbc4e137942fcb5527df0db533f5b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=ooSQy%2BMonvjaVsOcbZrfIoW8rAg%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2. 分析歌单下各歌曲关键信息</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了下载<span style="color: black;">位置</span>后一切都变得简单了,只需要做的是,获取你<span style="color: black;">喜欢</span>的歌单<span style="color: black;">位置</span>,<span style="color: black;">而后</span>将此歌单下所有歌曲id进行爬取!还记得爬取的<span style="color: black;">过程</span>吗?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>要<span style="color: black;">知道</span>自己想要的信息,而笔者<span style="color: black;">这儿</span>想要的信息就三个:<strong style="color: blue;">歌单名<span style="color: black;">叫作</span>、歌曲名<span style="color: black;">叫作</span>、歌曲id。</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>接下来就对网页分析下相应的节点。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">歌单名<span style="color: black;">叫作</span>,看不清楚手机<span style="color: black;">能够</span>点开放大看,以下<span style="color: black;">照片</span>均可:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/1ecdeac61004431a9b066ad4ff4608f2~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=t%2Be46CON3uN%2BgwO%2BeXjY3leVvzg%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">歌曲名<span style="color: black;">叫作</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/825465eacf8143ffb8e82765767e00f6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=fGALdRrlMCC6Kpx0IyrUSWzTS14%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">歌曲id:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/e03521eb5c394aaea1a8d2e0e7ff3b13~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=m9QGGcMPXXyP0iyVJM%2FbP787B04%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3. 爬取歌单下所有歌曲的id,<span style="color: black;">同期</span>传入外链<span style="color: black;">位置</span>,进行下载</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了以上分析后,只需要验证下,<span style="color: black;">咱们</span>用 Python 模拟请求,<span style="color: black;">拜访</span>此网页时,返回的信息与<span style="color: black;">咱们</span>F12分析后的结果<span style="color: black;">是不是</span>一致。<span style="color: black;">是不是</span>真的存在F12看到的信息节点呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网页<span style="color: black;">位置</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/f1283bc4f60b4849b5f5e50ad74e00d1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=NRNoi7N8kQOSwIlydee%2FONm%2FHow%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此处<span style="color: black;">运用</span> requests 库模拟发送请求,得到返回的数据,代码是<span style="color: black;">这般</span>的:</p>import requests
headers = {
user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/69.0.3497.100 Safari/537.36
}
url = "https://music.163.com/#/playlist?id=2722916074"
r = requests.get(url=url,headers=headers)
print(r.text)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用上面这段代码,你请求后返回的内容,会惊奇的<span style="color: black;">发掘</span>,怎么找<span style="color: black;">亦</span>找不到与F12看到的<span style="color: black;">类似</span>节点内容!<span style="color: black;">为何</span>呢?猜测是<span style="color: black;">由于</span>网易云做了一层<span style="color: black;">位置</span>上的反爬机制,<span style="color: black;">隐匿</span>了真正的歌单网页。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">打开F12,再来找下请求:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/ae0050aa48d54de19d54275bf4427449~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729530762&x-signature=XbZhQAQ4D8TRAx6nRJ2DV%2FGAS5E%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">经<span style="color: black;">查询</span>,确实<span style="color: black;">隐匿</span>了真实<span style="color: black;">位置</span>,<span style="color: black;">此刻</span>浏览器上看到的 url 多了一个#,<span style="color: black;">因此</span>当<span style="color: black;">咱们</span>请求图中 3 的<span style="color: black;">位置</span>时,便是返回的与F12看到的节点信息一致。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思考与动手</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本篇<span style="color: black;">文案</span>,大致的思路<span style="color: black;">已然</span>带<span style="color: black;">大众</span>梳理清楚了,接下来<span style="color: black;">便是</span>动手了!笔者<span style="color: black;">这次</span>不打算将自己写的代码放出来,<span style="color: black;">大众</span>对学习爬虫感兴趣的,不妨自己动手写下程序试试。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思路有了,剩下的<span style="color: black;">便是</span>编程实战了!算是给<span style="color: black;">大众</span>留个小作业,有时间有兴趣的,<span style="color: black;">能够</span>评论区留言提问。写完的<span style="color: black;">朋友</span>,<span style="color: black;">能够</span>自己动手验证下,<span style="color: black;">是不是</span>能将音乐下载到本地,此次案例是一个不错的练手小项目!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下一期,笔者会写一篇讲解自己是<span style="color: black;">怎样</span>设计代码的,顺便在把代码开源出来,敬请期待!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">PS:<span style="color: black;">文案</span>首发公众号哟,感兴趣的欢迎关注:migezatan.(咪哥杂谈)</strong></p>
回顾历史,我们感慨万千;放眼未来,我们信心百倍。 i免费外链发布平台 http://www.fok120.com/
页:
[1]