【Python爬虫】【已更新】酷狗音乐Flac无损音乐下载地址获取
本帖最后由 小涩席 于 2020-3-28 16:49 编辑如题:还是那句话,自从笔者这几天迷上听音乐以后,就到处找高质的音乐听,
奈何现在所有的高质音乐都不方便下载,结果就搞了一个下载酷狗音乐的代码,
把代码复制到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)
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.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)
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
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.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)
``` 你看看评论区一大片在问问题的就已经能证明了你的教程根本不是给大众用的,要不然也不会提出这么多的问题了,你就不想想论坛里面是有多少用户是做技术工作的??能懂软件和代码的寥寥无几,都是一些其它行业的人,隔行如隔山,你整教程又不整完整,用到什么工具?怎么用?你从头到尾的都在自说自话,这就是你不考虑实际用户知识水平有限,根本看不懂,所以你的热心也是寥寥无几。:lol 本帖最后由 老飞机 于 2020-3-31 10:00 编辑
楼主,这个是我写的,解析接口的链接有时效性的
支持输入序号进行下载歌曲,就是接口很快失效
import requests
import json
import os
from urllib.request import urlopen
from tqdm import tqdm
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
music_list = []
def GetitemInfo():
search = input("\033[1;45m请输入想搜索的歌\033[0m -==>:")
url1 = 'https://songsearch.kugou.com/song_search_v2?callback=jQuery112404852453033521309_1585374178035&keyword={}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1585374178039'.format(search)
r = requests.get(url1, headers=headers)
r.encoding = r.apparent_encoding
JsonText = json.loads(str(r.text).strip('jQuery112404852453033521309_1585374178035(')[:-2])
contents1 = []
tone_quality = input("请选择音质;\033[1;45m1\033[0m = 无损音质 , \033[1;45m2\033[0m = 流畅音质 :\n")
if tone_quality == "1":#高音质
for i in JsonText["data"]["lists"]:
quality_type = {}
SQFileHash = i['SQFileHash']
AlbumID = i['AlbumID']
downurl = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191012215974743400859_1585619590127&hash={}&album_id={}&dfid=0zI24KbQfP0cPOWD2MSu40&mid=415d42b77b039a48daf4f63d5b411ceb&platid=4&_=1585619590128'.format(SQFileHash, AlbumID)
quality_type['type'] = '.flac'
quality_type['url'] = downurl
contents1.append(quality_type)
if tone_quality == "2": #流畅音质
for i in JsonText["data"]["lists"]:
quality_type = {}
AlbumID = i['AlbumID']
FileHash = i['FileHash']
downurl1 = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191012215974743400859_1585619590127&hash={}&album_id={}&dfid=0zI24KbQfP0cPOWD2MSu40&mid=415d42b77b039a48daf4f63d5b411ceb&platid=4&_=1585619590128'.format(FileHash, AlbumID)
quality_type['type'] = '.mp3'
quality_type['url'] = downurl1
contents1.append(quality_type)
return contents1
def parser_json(data):
ss = 0
for data in data:
url = data['url']
ss += 1
try:
response = requests.get(url,headers=headers)
if 'play_url' in response.text:
music = {}
json_TEXt = json.loads(str(response.text).strip("jQuery19108584376284926096_1585328784250(")[:-2])['data']
url = json_TEXt['play_url']
url1 = json_TEXt['play_backup_url']
music_mane = json_TEXt['album_name']
singer = json_TEXt['author_name']
music["音乐链接"] = url
music["歌曲名字"] = music_mane
music["歌手"] = singer
music['歌曲类型'] = data['type']
print("音乐链接:",url)
print("备份音乐链接:",url1)
print("歌曲名字:",music_mane)
print("歌手:",singer)
print("下载歌曲的序号:","\033[1;45m {} \033[0m".format(ss))
print("\033[1;45m=\033[0m"*41,"\033[1;45m分割线\033[0m","\033[1;45m=\033[0m"*41)
music_list.append(music)
else:
print('\n该URL解析不了',"或者出现滑动验证提示:\nhttps://www.kugou.com/song/#hash=C9B86DA7FE6F500D72A68FF7A705E1E0&album_id=12855831")
continue
except Exception as e:
print(e)
if_download = input("请输入上面的\033[1;45m下载歌曲的序号\033[0m:")
slice = music_list
download_from_url(slice)
def download_from_url(music_data):
for data in music_data:
url = data['音乐链接']
music_mane = data['歌曲名字']
singer = data['歌手']
quality_type = data['歌曲类型']
dst = singer + " - " +music_mane + quality_type
file_size = int(urlopen(url).info().get('Content-Length', -1))
if os.path.exists(dst):
first_byte = os.path.getsize(dst)
else:
first_byte = 0
if first_byte >= file_size:
return file_size
header = {"Range": "bytes=%s-%s" % (first_byte, file_size)}
pbar = tqdm(
total=file_size, initial=first_byte,
unit='B', unit_scale=True, desc=dst)
req = requests.get(url, headers=header, stream=True)
with(open( dst, 'ab')) as f:
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(1024)
pbar.close()
return file_size
if __name__ == '__main__':
parser_json(GetitemInfo())
楼主你看,跟你的歌曲是是一样的,解析不出来无损音质 厉害了我的哥 耳机推荐网易云的HiFi 像这种的一般多久会失效 代码在哪里?要跟着敲一次吗 建议加一个歌曲名称的爬取,直接把文件命名成歌曲-歌手的形式 能爬索尼hires吗? 500以内首推shp9500,500到1000就7506和srh840,如果木耳这些个就够了再贵也没啥必要了,如果预算够请直接上大奥2代 建议加一个歌曲名称的爬取,直接把文件命名成歌曲-歌手的形式 先感谢大佬分享这只爬虫,借走下音乐了,哈哈 嗯,这个不错,有好听的歌曲听了