Python爬取酷狗音乐TOP500 !

作者: 14e61d025165 | 来源:发表于2019-04-26 15:44 被阅读0次

发这个主要是因为我本地没有歌,有的歌还是VIP下载不了,平时听歌还得用流量。所以就想着看能直接把所有的歌曲直接拿下来。就去看了酷狗的主页面。想直接拿到TOP500.因为没找到怎么去下载,然后就在网上找了一下,找到了一个根据hash拼接url,下载歌曲。,只要找到hash值就啥都解决了。

看了之后发现还不是特别难弄,所以就直接趴下来了。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556264619765 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

上面是网址,

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556264619770 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

改变数字就可以实现翻页,所以这个不能翻页的问题解决了。然后就是老套路按F12查看找network.

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556264619772 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

完整项目地址 可以添加QQ源码群1004391443,有飞机大战、颜值打分器、打砖块小游戏、红包提醒神器、小姐姐表白神器等具体的实训项目,有清晰源码,有相应的文件

往下翻,发现这些都有注释,那就更好办了。

解析这个数据,拿出来hash值和filename,歌词lyric。

也没什么要说的了,直接贴代码

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import requests
from lxml import etree
import json
import re
import os
class kugou():
def startkugou(self):
for i in range(23, 24):
print(i)
res = requests.get('https://www.kugou.com/yy/rank/home/%s-8888.html?from=rank' % str(i))
self.get_song(res)
def get_song(self, res):
html = etree.HTML(res.content.decode('utf8'))
content = html.xpath('//script[10]')
content2 = content[0].text
# 解析出json列表,类型是str
content1 = content2.split('global.features =')[1].split('(function()')[0].strip()[0:-1]
try:
# 转换成json数据
content = json.loads(content1)
for i in range(len(content)):
hash = content[i]["Hash"]
file_name = content[i]["FileName"]
hash_url = "http://www.kugou.com/yy/index.php?r=play/getdata&hash=" + hash
hash_content = requests.get(hash_url)
play_url = ''.join(re.findall('"play_url":"(.?)"', hash_content.text))
lyrics = ''.join(re.findall('"lyrics":"(.
?)"', hash_content.text))
real_download_url = play_url.replace("\", "")
try:
# if os.path.exists('kugou/' + file_name + '.txt'):
# print(file_name + " 歌词已经存在")
# # continue
# else:
with open('kugou/' + file_name + '.txt', 'w', encoding='utf8')as f:
f.write(lyrics.encode('utf8').decode('unicode_escape'))
print(file_name + "歌词已下载完成!")
# if os.path.exists('kugou/' + file_name + '.mp3'):
# print(file_name+" 歌曲已经存在")
# # continue
# else:
with open('kugou/' + file_name + ".mp3", "wb")as fp:
fp.write(requests.get(real_download_url).content)
print(file_name + "歌曲已下载完成!")
except OSError as e:
print("出现异常" + file_name)
file_name = self.validateTitle(file_name)
# if os.path.exists('kugou/' + file_name + '.txt'):
# print(file_name + " 歌词已经存在")
# # continue
# else:
with open('kugou/' + file_name + '.txt', 'w', encoding='utf8')as f:
f.write(lyrics.encode('utf8').decode('unicode_escape'))
print(file_name + "歌词已下载完成!")
# if os.path.exists('kugou/' + file_name + '.mp3'):
# print(file_name + " 歌曲已经存在")
# # continue
# else:
with open('kugou/' + file_name + ".mp3", "wb")as fp:
fp.write(requests.get(real_download_url).content)
print(file_name + "歌曲已下载完成!")
except json.decoder.JSONDecodeError as e:
print(e)
print(content2)
content1 = content2.split('global.features =')[1].strip().split('(function() {')[0].strip()
content1 = content1[0:-1]
print(content1)
def validateTitle(self, file_name):
""" 将 title 名字 规则化
:param title: title name 字符串
:return: 文件命名支持的字符串 """
rstr = r"[=(),/\:*?"<>|' ']" # '= ( ) , / \ : * ? " < > | ' 还有空格
new_title = re.sub(rstr, "_", file_name) # 替换为下划线
return new_title
if name == 'main':
kugou().startkugou()
</pre>

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556264619833 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

代码基本都有注释,有问题可以留言,谢谢!

相关文章

  • python第四天(一)BeautifulSoup爬虫

    爬取猫眼top100数据 爬取酷狗top500数据

  • Python爬取酷狗音乐TOP500 !

    发这个主要是因为我本地没有歌,有的歌还是VIP下载不了,平时听歌还得用流量。所以就想着看能直接把所有的歌曲直接拿下...

  • Python爬取酷狗音乐

    在常见的几个音乐网站里,酷狗可以说是最好爬取的啦,什么弯都没有,也没加密啥的,所以最适合小白入门爬虫 本篇针对爬虫...

  • python爬虫教程:爬取酷狗音乐

    本篇针对爬虫零基础的小白,所以每一步骤我都截图并详细解释了,其实我自己看着都啰嗦,归根到底就是两个步骤的请求,还请...

  • python爬虫教程:爬取酷狗音乐 !

    在常见的几个音乐网站里,酷狗可以说是最好爬取的啦,什么弯都没有,也没加密啥的,所以最适合小白入门爬虫 本篇针对爬虫...

  • Python爬取酷狗音乐py文件

    运行后直接输入想找的歌名,选择搜索结果下载文件到本地桌面。 运行前需要安装相关库。只支持下载128kbps的MP3...

  • 爬取酷狗TOP500的数据

    不废话,直接上图 先观察一下要爬取的界面,每页有22首歌。信息为:排名,歌手,歌名以及时长。URL链接http:/...

  • 用Python爬取酷我音乐

    作者:il_持之以恒_li https://blog.csdn.net/qq_45404396/article/d...

  • Java爬取并下载酷狗TOP500歌曲

    下文方法及代码仅供学习使用,不做他用。示例用到了一些库,包括:jsoup、HttpClient、net.sf.js...

  • 流量时代,何去何从

    不知道从什么时候起,歌坛变了。 唱片悄悄退出市场,短视频平台的音乐爆火。 酷狗音乐TOP500的前10首,其中几乎...

网友评论

    本文标题:Python爬取酷狗音乐TOP500 !

    本文链接:https://www.haomeiwen.com/subject/ejcqnqtx.html