Python爬虫下载酷狗音乐 | 您所在的位置:网站首页 › 酷狗音乐怎么免费下载付费音乐文件 › Python爬虫下载酷狗音乐 |
目录1、Python下载酷狗音乐1.1、前期准备1.2、分析1.2.1、第一步1.2.2、第二步1.2.3、第三步1.2.4、第四步1.3、代码实现1.4、运行结果
1、Python下载酷狗音乐
1.1、前期准备
要有rquests、re、json包,如果不存在,先用pip install安装 1.2、分析 1.2.1、第一步首先我们先进入首页并搜索你要查询的歌手,进入查询的页面,查看页面源代码,发现并没有这个列表数据,这里就不展示了,页面源代码没有发现想要的数据,那就来看network网络请求中的数据 1.2.2、第二步打开网络请求,刷新页面,找到需要的请求
随便点击一首歌曲,进入听歌页面,发现还是老套路,在页面源代码中没有发现需要的MP3音乐文件,所有又需要看network网络请求中的数据
通过分析可以知道,第二步中的请求中有我们需要的这两个参数,如下
访问找的两个求,查看返回的数据信息 第二步的请求
第三步的请求
至此所有的分析都完成了,可以直接上代码了 1.3、代码实现 import requests import json import re url = "https://searchrecommend.kugou.com/get/complex?callback=jQuery112409589811716312686_1626852436130&word=周杰伦&_=1626852436132" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.70", "referer": "https://www.kugou.com/" } resp = requests.get(url, headers=headers) p1 = re.compile(r'jQuery.*?[({].*?"data":.*?"song":(.*?),"correctiontip"', re.S) songs = re.findall(p1, resp.text)[0] songs = json.loads(songs) for song in songs: title = song['songname'] AlbumID = song['AlbumID'] song_hash = song['hash'] child_url = f"https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191053318263960215_1626866592344&hash={song_hash}&dfid=1tXkst0i97Rq4RW0pz15GjrP&mid=3196606d7d3ff0207a473da58e0b44b3&platid=4&album_id={AlbumID}&_=1626866592346" child_resp = requests.get(child_url, headers=headers) child_resp.encoding="utf-8" obj = re.compile(r'jQuery.*?[({].*?"data":(.*?)}[)]', re.S) child_song = re.findall(obj, child_resp.text)[0] child_song = child_song.replace("\\", "") child_song_src = json.loads(child_song)["play_url"] child_song_resp = requests.get(child_song_src) with open("音乐/"+title+".mp3", mode="wb") as f: f.write(child_song_resp.content) print(title+"下载完成!!!") print("okok") 1.4、运行结果下载周杰伦的歌,改变url去查询你喜欢的歌手
在本地文件中查看
|
CopyRight 2018-2019 实验室设备网 版权所有 |