freepd音乐爬虫下载Python手把手教程
前言
freepd网站,上面有很多无版权的音乐可供广大内容创作者使用,主页情况如下图:

网站提供一键打包下载的功能,但需要付钱购买,作为程序员穷屌丝一枚,还是尝试能不能从其他路径获取音乐,于是就想到了爬虫,可以通过爬虫将所有的音乐下载下来。
其实讲到爬虫,可能各位看官觉得爬虫是特别高大上的,比如鼎鼎大名的scrapy框架,但实际上爬虫往往可以根据自己的实际需求编写,直接使用python编写就能实现目的。所以这里介绍了使用python编写一段代码(我称之为爬虫)下载freepd音乐的代码。
目标分析
要通过爬虫代码自动化的下载我们需要的内容,首要的步骤就是了解下载内容的网站,了解被下载内容是如何组织的,在浏览器中可以按F12进入开发者模式,通过查看网络访问行为以及网络数据,找到被下载数据的规律。
在我看来没有规律的数据也就没有使用爬虫的必要。
下面我们一起来观察分析网页。
主页
从主页能够看到freepd对音乐大致做了分类:
子页面
我们选择其中一个点击进入:
从图中可知在该页面上有3个点需要注意:
- URL的变化,在不同分类页面中的url是有变化的:https://freepd.com/upbeat.php
- 我们的目的是下载音乐,将鼠标移动到下载按钮后可以看到下载该音乐的下载链接:https://freepd.com/music/Advertime.mp3
再次查看其它子页面,大家应该可以发现freepd的规律。
批量音乐名称的来源
通过对子页面的分析可以知道,通用页面音乐的下载链接模板为:https://freepd.com/music/{music_name}.mp3 ,page2页面对于的音乐下载链接模板为:https://freepd.com/Page2/music/{music_name}.mp3。
剩下的问题就是音乐的名称music_name是怎么来的,这个时候就需要F12进入开发者模式了,分析网络数据,如下:
通过浏览器的网络请求数据分析定位到了会返回所有音乐名称的请求包,然后在该请求包中搜索一个界面有显示的歌曲的名称,比如Advertime,就能够定位到该歌曲在返回内容中的格式,通过多查询几首歌曲,找到歌曲在返回值中的规律是:HREF="/music/Advertime.mp3"(在Page2中稍有不同)
其实到这里的分析阶段已经差不多了,可以明确的是歌曲的下载链接和歌名的形式都是规律的,在这个基础上就可以开始编写代码了。
代码+讲解部分
引用必要模块
1 | import requests |
- requests模块是进行网络访问必要的包,我们这里主要使用该包提供的get()方法,获取对应url的网络访问内容。
- re是正则表达式模板,这里使用该模块的原因是通过分析freepd的网页返回内容知道,所有待下载的音乐都是和html内容混合到一起的,不是传统的json或xml标准格式的返回内容,所以需要使用re库提取结果中的音乐名称。
- os库目的是音乐文件存储和判断的库。
- threading库通过并发的方式提高下载的效率。
获取歌曲名称信息
1 | part = "epic" |
确定想要下载的歌曲属于哪个子页面,通过变量定义,更加方便。
1 | if not part: |
通过定义的变量不同进而确定不同的页面url、下载链接url以及文件存储路径,并使用requests库获取url对于的内容,并使用正则表达式库从所有返回值中提取歌名。
经过这段代码后,最关键的内容是baseURL用来拼接下载链接,re_result保存的是正则表达式返回的歌曲名称内容,path是歌曲存放的路径。
定义下载方法和创建线程类
1 | class DownloadThread(threading.Thread): |
- 定义一个下载音乐的方法,通过for循环依次下载歌曲,如果歌曲存在则直接跳过,防止重复下载,下载错误时打印异常消息。其实有下载错误的歌曲也不会影响,重新再执行一次代码即可。
- 创建一个线程类,用户并行下载,提高效率。
创建多线程,并等待下载完成
1 | thread_num = 5 |
Thread create..........
[b'Adventure', b'Adventures of Flying Jack', b'Ancient Rite', b'Assassin', b'Battle Ready', b'Behind Enemy Lines', b'Beyond - part 2']
************************
Thread create..........
[b'Big Eyes', b'Black Knight', b'Cornfield Chase', b'Emotional Blockbuster 2', b'Epic Blockbuster 2', b'Epic Boss Battle', b'Evil Incoming']
************************
Thread create..........
[b'Fanfare X', b'Go On Without Me', b'Gothamlicious', b'Heroic Adventure', b'Hit n Smash', b'Honor Bound', b'Kings Trailer']
************************
Thread create..........
[b'Lonely Mountain', b'Mothership', b'New Hero in Town', b'Night Attack', b'Night Vigil', b'Overture', b"Putin's Lullaby"]
************************
Thread create..........
[b'Release the Hybrids', b'Rulers of Our Lands', b'Strength of the Titans', b'The Enemy', b'The Ice Giants', b'The Range', b'Think About It']
************************
Thread create..........
[b'Video Game Blockbuster']
************************
File Adventure.mp3 exists, skip
File Big Eyes.mp3 exists, skip
File Fanfare X.mp3 exists, skip
File Adventures of Flying Jack.mp3 exists, skip
File Release the Hybrids.mp3 exists, skip
File Video Game Blockbuster.mp3 exists, skip
File Lonely Mountain.mp3 exists, skip
File Black Knight.mp3 exists, skip
File Go On Without Me.mp3 exists, skip
File Rulers of Our Lands.mp3 exists, skip
File Ancient Rite.mp3 exists, skip
File Mothership.mp3 exists, skip
File Cornfield Chase.mp3 exists, skip
File Gothamlicious.mp3 exists, skip
File Strength of the Titans.mp3 exists, skip
File Assassin.mp3 exists, skip
File New Hero in Town.mp3 exists, skip
File Emotional Blockbuster 2.mp3 exists, skip
File Heroic Adventure.mp3 exists, skip
File The Enemy.mp3 exists, skip
File Battle Ready.mp3 exists, skip
File Night Attack.mp3 exists, skip
File Epic Blockbuster 2.mp3 exists, skip
File Hit n Smash.mp3 exists, skip
File The Ice Giants.mp3 exists, skip
File Behind Enemy Lines.mp3 exists, skip
File Night Vigil.mp3 exists, skip
File Epic Boss Battle.mp3 exists, skip
File Honor Bound.mp3 exists, skip
File The Range.mp3 exists, skip
File Beyond - part 2.mp3 exists, skip
File Overture.mp3 exists, skip
File Evil Incoming.mp3 exists, skip
File Kings Trailer.mp3 exists, skip
File Think About It.mp3 exists, skip
File Putin's Lullaby.mp3 exists, skip
Download END
预先定义好线程的数量,这个数量稍微定大些会比较好,因为瓶颈主要是在服务器处(应该是对单个音乐下载有速度限制),从这个网站下载的速率来看,定50个线程也是可以的。
然后给每个线程分配对应数量的歌名,并启动下载,然后等待所有线程下载完成,Done!