【Python爬虫】【已更新】酷狗音乐Flac无损音乐下载地址获取
如题:还是那句话,自从笔者这几天迷上听音乐以后,就到处找高质的音乐听,
奈何现在所有的高质音乐都不方便下载,结果就搞了一个下载酷狗音乐的代码,
把代码复制到Pycharm中运行就行了,结束后能够得到无损音乐的下载地址,
希望能够帮助到和我一样的同类人。
顺便问一下,有推荐好一点的耳机吗?垃圾耳机听起来效果不行。(手动难受)
# -*- coding : 'UTF-8' -*-
# Author:XSX
# Python3.8 Pycharm
import requests
from urllib import parse
from GetRandomheader import Randomheader
import re
import json
import time
def URLDecode(text):
Decode = parse.quote(text)
print('正在查找【{}】的无损音乐------请等待'.format(text))
return Decode
def GetitemInfo(Decodetext, headers):
ItemInfos = []
url1 = 'https://songsearch.kugou.com/song_search_v2?callback=jQuery1124047909927884881864_1584769239146&keyword={}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1584769239150'.format(Decodetext)
r = requests.get(url1, headers=headers)
r.encoding = r.apparent_encoding
JsonText = json.loads(r.text[43:-2])
for i in JsonText["data"]["lists"]:
contents = {}
contents['SongName'] = i['SongName']
contents['AlbumID'] = i['AlbumID']
contents['FileHash'] = i['FileHash']
contents['SQFileHash'] = i['SQFileHash']
contents['HQFileHash'] = i['HQFileHash']
contents['MvHash'] = i['MvHash']
ItemInfos.append(contents)
return ItemInfos
def GetDownLinks(ItemInfos, headers):
try:
DownLists = []
for Iteminfo in ItemInfos:
ID = Iteminfo['AlbumID']
Hash = Iteminfo['SQFileHash']
downurl = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191006287980471670584_1584780452839&hash={0}&album_id={1}&dfid=3dTk9E0IfcV50gyRKr3OGZey&mid=0407f15d60c01b0ccb16bf2323d904b2&platid=4&_=1584780452841'.format(Hash, ID)
time.sleep(3)
r1 = requests.get(downurl, headers=headers)
r1.encoding = r1.apparent_encoding
DownLists.append(r1.text[42:-2].replace('\\', '').encode('utf8').decode('unicode_escape'))
print('正在解析无损音质歌曲' + '\n' + '==========================' + '\n' + '说明:解析后无下载地址是因为该歌曲没有无损音质或需要付费下载' + '\n' + '==========================' + '>>>>>>>请稍后<<<<<<<')
return DownLists
except:
pass
def GetSongLinks(DownLists):
try:
n = 0
for DownList in DownLists:
link = re.findall(r'.*' + '"play_url":"' + '(.*)' + '","authors"', str(DownList))
n += 1
print('正在解析第【{}】首无损音乐地址!'.format(n) + '\n' + '请点击下方链接下载保存音乐>>>>>>>>')
print(link)
except:
pass
if __name__ == '__main__':
head = Randomheader()
GetSongLinks(GetDownLinks(GetitemInfo(URLDecode(input('请输入需要搜索的歌曲名称:')), head), head))
下面是更新后的解析代码,优化了解析速度,去除了无用的歌曲列表,旨在更快得到无损音质下载地址!!!!
有需求自取,运行方法一样。
# -*- coding : 'UTF-8' -*-
# python3.8 Pycharm
# Author : XSX
import requests
from urllib import parse
from GetRandomheader import Randomheader
import re
import json
def URLDecode(text):
Decode = parse.quote(text)
print('正在查找【{}】的无损音乐'.format(text))
return Decode
def GetitemInfo(Decodetext, headers):
ItemInfos = []
url1 = 'https://songsearch.kugou.com/song_search_v2?callback=jQuery1124047909927884881864_1584769239146&keyword={}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1584769239150'.format(Decodetext)
r = requests.get(url1, headers=headers)
r.encoding = r.apparent_encoding
JsonText = json.loads(r.text[43:-2])
for i in JsonText["data"]["lists"]:
contents = {}
contents['SongName'] = i['SongName']
contents['AlbumID'] = i['AlbumID']
contents['FileHash'] = i['FileHash']
contents['SQFileHash'] = i['SQFileHash']
contents['HQFileHash'] = i['HQFileHash']
contents['MvHash'] = i['MvHash']
ItemInfos.append(contents)
return ItemInfos
def GetDownLinks(ItemInfos, headers):
try:
DownLists = []
Iteminfo = ItemInfos[0]
ID = Iteminfo['AlbumID']
Hash = Iteminfo['SQFileHash']
downurl = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191006287980471670584_1584780452839&hash={0}&album_id={1}&dfid=3dTk9E0IfcV50gyRKr3OGZey&mid=0407f15d60c01b0ccb16bf2323d904b2&platid=4&_=1584780452841'.format(
Hash, ID)
r1 = requests.get(downurl, headers=headers)
r1.encoding = r1.apparent_encoding
DownLists.append(r1.text[42:-2].replace('\\', '').encode('utf8').decode('unicode_escape'))
print('正在解析无损音质歌曲' + '\n' + '==========================' + '\n' + '说明:解析后无下载地址是因为该歌曲没有无损音质或需要付费下载' + '\n' + '==========================' + '\n' + '>>>>>>>请稍后<<<<<<<')
return DownLists
except:
print("Process Error!")
def GetSongLinks(DownLists, text):
try:
for DownList in DownLists:
link = re.findall(r'.*' + '"play_url":"' + '(.*)' + '","authors"', str(DownList))
print('下发为【{}】无损音乐地址!'.format(text) + '\n' + '请点击下方链接下载保存音乐>>>>>>>>')
print(link)
except:
pass
if __name__ == '__main__':
head = Randomheader()
text = input('请输入需要搜索的歌曲名称:')
GetSongLinks(GetDownLinks(GetitemInfo(URLDecode(text), head), head), text)
-
音质1
-
音质2
-
代码运行后
微信赞赏支付宝赞赏