Python爬虫全网搜索并下载音乐
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><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>。今天的最早的<span style="color: black;">需要</span>是来自于 </span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">怎样</span>免费[白嫖]下载全网<span style="color: black;">优秀</span>音乐</span></span><span style="color: black;"> ,我去b站上面搜索到了一个大牛做过的一个歌曲搜索神器,界面是<span style="color: black;">这般</span>的:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然在学习Python的道路上肯定会困难,<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>01<span style="color: black;">就可</span>获取<span style="color: black;">海量</span>Python学习资料</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/bfc0cc660cfb432f905412040a7f9fcf~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729532552&x-signature=iNGZ4efw87HZfTawTR5l1YTrSRQ%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 style="color: black;">确实很好用的,<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>,爬虫只能爬取某一页<span style="color: black;">或</span>某些页的网站资源,<span style="color: black;">因此</span>我很好奇它<span style="color: black;">背面</span>是怎么实现的?</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">笔者<span style="color: black;">始终</span><span style="color: black;">败兴</span>都是做的基于Python3.7版本的网络爬虫,<span style="color: black;">因此</span>本文<span style="color: black;">亦</span>是基于此来学习记录的。<span style="color: black;">首要</span>爬取的网站不是对应的音乐网站,而是一个 音乐直链搜索 ,那里汇聚了大<span style="color: black;">都数</span>音频音乐的解析功能,界面如下:</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/63adde0573a6408ab8c444583d2ab181~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729532552&x-signature=HrFzZgCQBHC8k32SbSETWfRDup4%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 style="color: black;"><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></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/e889f17f35c64ac893c440f6e476fad6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729532552&x-signature=l%2BHKckJMRe1Zd0JthvstD3Tjnm4%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 style="color: black;">解析的结果如上,获取的数据格式是 </span></span><span style="color: black;"><span style="color: black;">json</span></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 style="color: black;">源代码实现如下:</span></span></p><span style="color: black;">import</span> requests
<span style="color: black;">import</span> jsonpath
<span style="color: black;">import</span> os
<span style="color: black;">"""
1.url
2.模拟浏览器请求
3.解析网页源代码
4.<span style="color: black;">保留</span>数据
"""</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">song_download</span><span style="color: black;">(url,title,author)</span>:</span>
<span style="color: black;"># 创建文件夹</span>
os.makedirs(<span style="color: black;">"music"</span>,exist_ok=<span style="color: black;">True</span>)
path = <span style="color: black;">music\{}.mp3</span>.format(title)
print(<span style="color: black;">歌曲:{0}-{1},正在下载...</span>.format(title,author))
<span style="color: black;"># 下载(这种读写文件的下载方式适合少量文件的下载)</span>
content = requests.get(url).content
<span style="color: black;">with</span>open(file = title + author +<span style="color: black;">.mp3</span>,mode=<span style="color: black;">wb</span>) <span style="color: black;">as</span> f:
f.write(content)
print(<span style="color: black;">下载完毕,{0}-{1},请试听</span>.format(title,author))
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_music_name</span><span style="color: black;">()</span>:</span>
<span style="color: black;">"""
搜索歌曲名<span style="color: black;">叫作</span>
:return:
"""</span>
name = input(<span style="color: black;">"请输入歌曲名<span style="color: black;">叫作</span>:"</span>)
print(<span style="color: black;">"1.网易云:netease\n2.QQ:qq\n3.酷狗:kugou\n4.酷我:kuwo\n5.百度:baidu\n6.喜马拉雅:ximalaya"</span>)
platfrom = input(<span style="color: black;">"输入音乐平台类型:"</span>)
print(<span style="color: black;">"-------------------------------------------------------"</span>)
url = <span style="color: black;">https://music.liuzhijin.cn/</span>
headers = {
<span style="color: black;">"user-agent"</span>:<span style="color: black;">"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"</span>,
<span style="color: black;"># 判断请求是异步还是同步</span>
<span style="color: black;">"x-requested-with"</span>:<span style="color: black;">"XMLHttpRequest"</span>,
}
param = {
<span style="color: black;">"input"</span>:name,
<span style="color: black;">"filter"</span>:<span style="color: black;">"name"</span>,
<span style="color: black;">"type"</span>:platfrom,
<span style="color: black;">"page"</span>: <span style="color: black;">1</span>,
}
res = requests.post(url=url,data=param,headers=headers)
json_text = res.json()
title = jsonpath.jsonpath(json_text,<span style="color: black;">$..title</span>)
author = jsonpath.jsonpath(json_text,<span style="color: black;">$..author</span>)
url = jsonpath.jsonpath(json_text,<span style="color: black;">$..url</span>)
<span style="color: black;">if</span> title:
songs = list(zip(title,author,url))
<span style="color: black;">for</span> s <span style="color: black;">in</span> songs:
print(s[<span style="color: black;">0</span>],s[<span style="color: black;">1</span>],s[<span style="color: black;">2</span>])
print(<span style="color: black;">"-------------------------------------------------------"</span>)
index = int(input(<span style="color: black;">"请输入您想下载的歌曲版本:"</span>))
song_download(url,title,author)
<span style="color: black;">else</span>:
print(<span style="color: black;">"对不起,暂无搜索结果!"</span>)
<span style="color: black;">if</span> __name__ == <span style="color: black;">"__main__"</span>:
get_music_name()<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">演示一下运行效果:</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/ca1305f0e14146aaa963e29863992585~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729532552&x-signature=HL139HU1A5bQZCk8j7XpZ%2FKSq98%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 style="color: black;"><span style="color: black;">而后</span>文件下就会<span style="color: black;">显现</span>对应的歌曲 </span></span><span style="color: black;"><span style="color: black;">.mp3</span></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/3b32f92d63514fefbc85da566ee63f9f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729532552&x-signature=qaVUJb4TrmwJLHNVdQ1njO07hH4%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 style="color: black;">并且每次的歌曲检索都是在PyCharm专业版的<span style="color: black;">掌控</span>台中进行的,<span style="color: black;">这般</span>的用户体验就非常糟糕,<span style="color: black;">因此</span>针对以上两个问题,我对源代码进行了改进。</span></span></p><span style="color: black;"># 导入模块</span>
<span style="color: black;">from</span> tkinter <span style="color: black;">import</span> *
<span style="color: black;">import</span> requests
<span style="color: black;">import</span> jsonpath
<span style="color: black;">import</span> os
<span style="color: black;">from</span> urllib.request <span style="color: black;">import</span> urlretrieve
<span style="color: black;"># 2.功能实现</span>
<span style="color: black;">"""
1.url
2.模拟浏览器请求
3.解析网页源代码
4.<span style="color: black;">保留</span>数据
"""</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">song_download</span><span style="color: black;">(url,title,author)</span>:</span>
<span style="color: black;"># 创建文件夹</span>
os.makedirs(<span style="color: black;">"music"</span>,exist_ok=<span style="color: black;">True</span>)
path = <span style="color: black;">music\{}.mp3</span>.format(title)
text.insert(END,<span style="color: black;">歌曲:{0}-{1},正在下载...</span>.format(title,author))
<span style="color: black;"># 文本框滑动</span>
text.see(END)
<span style="color: black;"># 更新</span>
text.update()
<span style="color: black;"># 下载</span>
urlretrieve(url,path)
text.insert(END,<span style="color: black;">下载完毕,{0}-{1},请试听</span>.format(title,author))
<span style="color: black;"># 文本框滑动</span>
text.see(END)
<span style="color: black;"># 更新</span>
text.update()
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_music_name</span><span style="color: black;">()</span>:</span>
<span style="color: black;">"""
搜索歌曲名<span style="color: black;">叫作</span>
:return:
"""</span>name = entry.get()
platfrom = var.get()<span style="color: black;"># name = 白月光与朱砂痣</span>
url = <span style="color: black;">https://music.liuzhijin.cn/</span>
headers = {
<span style="color: black;">"user-agent"</span>:<span style="color: black;">"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"</span>,
<span style="color: black;"># 判断请求是异步还是同步</span>
<span style="color: black;">"x-requested-with"</span>:<span style="color: black;">"XMLHttpRequest"</span>,
}
param = {
<span style="color: black;">"input"</span>:name,
<span style="color: black;">"filter"</span>:<span style="color: black;">"name"</span>,
<span style="color: black;">"type"</span>:platfrom,
<span style="color: black;">"page"</span>: <span style="color: black;">1</span>,
}
res = requests.post(url=url,data=param,headers=headers)
json_text = res.json()
title = jsonpath.jsonpath(json_text,<span style="color: black;">$..title</span>)
author = jsonpath.jsonpath(json_text,<span style="color: black;">$..author</span>)
url = jsonpath.jsonpath(json_text, <span style="color: black;">$..url</span>)
print(title,author,url)
song_download(url[<span style="color: black;">0</span>],title[<span style="color: black;">0</span>],author[<span style="color: black;">0</span>])
<span style="color: black;"># 1.用户界面</span>
<span style="color: black;"># 创建画板</span>
root = Tk()
<span style="color: black;"># 设置窗口标题</span>
root.title(<span style="color: black;">全网音乐下载器</span>)
<span style="color: black;"># 设置窗口<span style="color: black;">体积</span>以及<span style="color: black;">显现</span>的位置</span>
root.geometry(<span style="color: black;">560x450+400+200</span>)
<span style="color: black;"># 标签组件</span>
label = Label(root,text=<span style="color: black;">"请输入下载的歌曲:"</span>,font=(<span style="color: black;">楷体</span>,<span style="color: black;">20</span>))
<span style="color: black;"># 定位与布局</span>
label.grid(row=<span style="color: black;">0</span>)
<span style="color: black;"># 输入框组件</span>
entry = Entry(root,font=(<span style="color: black;">宋体</span>,<span style="color: black;">20</span>))
entry.grid(row=<span style="color: black;">0</span>,column=<span style="color: black;">1</span>)
<span style="color: black;"># 单选按钮</span>
var = StringVar()
r1 = Radiobutton(root,text=<span style="color: black;">网易云</span>,variable=var,value=<span style="color: black;">netease</span>)
r1.grid(row=<span style="color: black;">1</span>,column=<span style="color: black;">0</span>)
r2 = Radiobutton(root,text=<span style="color: black;">QQ</span>,variable=var,value=<span style="color: black;">qq</span>)
r2.grid(row=<span style="color: black;">1</span>,column=<span style="color: black;">1</span>)
<span style="color: black;"># 列表框</span>
text = Listbox(root,font=(<span style="color: black;">楷体</span>,<span style="color: black;">16</span>),width=<span style="color: black;">50</span>,height=<span style="color: black;">15</span>)
text.grid(row=<span style="color: black;">2</span>,columnspan=<span style="color: black;">2</span>)
<span style="color: black;"># 下载按钮</span>button1 = Button(root,text=<span style="color: black;"><span style="color: black;">起始</span>下载</span>,font=(<span style="color: black;">楷体</span>,<span style="color: black;">15</span>),command=get_music_name)
button1.grid(row=<span style="color: black;">3</span>,column=<span style="color: black;">0</span>)
button2 = Button(root,text=<span style="color: black;">退出程序</span>,font=(<span style="color: black;">楷体</span>,<span style="color: black;">15</span>),command=root.quit)
button2.grid(row=<span style="color: black;">3</span>,column=<span style="color: black;">1</span>)
<span style="color: black;"># <span style="color: black;">表示</span>界面</span>
root.mainloop()
<span style="color: black;"># <span style="color: black;">怎样</span>将.py代码打包成.exe文件</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">咱们</span>再来<span style="color: black;">瞧瞧</span>效果吧</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/01af51cd0829445daabcb40dfe83f870~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729532552&x-signature=O1%2BGJaVrI57fkVLjGA9Bi6%2FKyEM%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 style="color: black;"><span style="color: black;">这般</span>加上了UI界面和部分代码优化之后,就<span style="color: black;">能够</span><span style="color: black;">经过</span> </span></span><span style="color: black;"><span style="color: black;">.py代码打包成.exe文件</span></span><span style="color: black;"> 发布给用户<span style="color: black;">运用</span>了。</span></p>
页:
[1]