当前位置:网站首页>一篇文章教会你使用Python网络爬虫下载酷狗音乐
一篇文章教会你使用Python网络爬虫下载酷狗音乐
2020-11-06 20:53:49 【Python进阶者】
【一、项目背景】
现在的听歌软件动不动就是各种付费,要下载软件才能听,当你下载了之后,你会惊奇的发现这首歌还收费,这就让一向喜欢白嫖的小编感到很伤心了。于是,小编冥思苦想,终于让我发现了其中的奥秘,一起来看看吧。
【二、项目准备】
1、编辑器:Sublime Text 3
2、软件:360浏览器
【三、项目目标】
下载我们喜欢的音乐。
【四、项目实现】
1、打开酷狗音乐官网
360浏览器打开酷狗音乐官网:
可以看到十分清爽的画风,这也是我比较喜欢的地方。
2、审查元素,分析请求
打开Network ,分析请求,我们可以看到:
从上图可以看出,这是请求的参数,所以我们可以使用Requests模块对它发起请求。
3、模拟发起请求
我们从网页中得知它的地址为:
https://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord=%E4%B8%8D%E8%B0%93%E4%BE%A0
可以看到真正对于我们来说有用的就只有SearchKeyWord参数后的值,前面的搜索类型默认填写即可,所以我们可以这样:
import requests
headers={
'accept': '*/*',
'accept-encoding':'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cookie': 'kg_mid=ebb2de813317a791bcf7b7d3131880c4; UM_distinctid=1722ba8b22632d-07ac0227c507a7-4e4c0f20-1fa400-1722ba8b2284a1; kg_dfid=0Q0BEI47P4zf0mHYzV0SYbou; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1590041687,1590280210,1590367138,1590367386; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1590367431',
'referer': 'https://www.kugou.com/yy/html/search.html',
'sec-fetch-mode': 'no-cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
aa=input('请输入歌名:')
data={
'callback': 'jQuery112408716317197794392_1590368232677',
'keyword':aa,
'page': '1',
'pagesize':'30',
'userid':'-1',
'clientver': '',
'platform': 'WebFilter',
'tag': 'em',
'filter': '2',
'iscorrection': '1',
'privilege_filter': '0',
'_': '1590368232679',
}
requests.get('https://www.kugou.com/yy/html/search.html',params=data,timeout=4)
这样就实现了模拟请求,我们来验证下:
可以看出它成功打印出了和我们上面一模一样的地址。
4、获取音乐文件列表
rep=requests.get('https://www.kugou.com/yy/html/search.html',params=data,timeout=5)
print(rep.url)
res=requests.get(rep.url,timeout=4)
print(res.text)
当我们将请求地址填写正确后,我发现竟然内容与预期不相符,但是请求地址对的一批。
我以为是这样的结果:
实际上的结果:
可以看到差距很大,而且用Json也根本获取不到,报格式错误,说明不是一个Json,看来这比QQ音乐难度高点。不过我们今天要下载的是音频文件,所以暂时跳过,不管它。
5、下载音频文件
我们在搜索后弹出来的列表中选择原唱曲目,进去听一下:
我选择第一首,打开是这样,我们开始骚操作,打开Network:
我们输入后缀Mp3,然后定位到对应的请求,然后打开它的请求结果,可以看到一个Json结果:
我们将Json结果粘贴到控制台,可以看到里面有一段关于Mp3的结果,不过添加了点干扰符号,我们把它提取出来:
这样我们就可以把酷狗音乐的歌曲下载下来了。
【五、项目总结】
1、其实,酷狗音乐与QQ音乐不同,酷狗音乐的下载链接更好捕获,你可以直接在它的播放界面捕获到:
模拟请求这个界面,一切都搞定了。
2、关于QQ音乐的获取,可以参考之前发布的系列文章:
3、需要本文源码的小伙伴,后台回复“酷狗音乐”四个字,即可获取。 想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/
版权声明
本文为[Python进阶者]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4521128/blog/4666153
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World