本文实现了酷我音乐的付费/免费音乐的下载功能,根据提示输入关键词即可下载。
# -*- coding: utf-8 -*- """================================================================================================================ @author : 爬虫王者 @function : 根据关键词下载酷我音乐 http://bd.kuwo.cn/ 包括付费、免费 ================================================================================================================""" import requests class KuwoMusic: def __init__(self): cookie = '_ga=GA1.2.1589894892.1671370061; _gid=GA1.2.735773706.1671370061; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1671370061; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1671371259; kw_token=UT8LIC4UWFE' self.headers = { 'Accept': 'application/json, text/plain, */*', 'Cookie': cookie, 'csrf': cookie.split('kw_token=')[-1].split(';')[0], 'Referer': 'http://bd.kuwo.cn/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' } self.path = '酷我音乐/' def search(self, keyword): url = 'http://bd.kuwo.cn/api/www/search/searchMusicBykeyWord' params = { 'key': keyword, 'pn': '1', 'rn': '30', 'httpsStatus': '1' } res = requests.get(url, params=params, headers=self.headers) print(res.status_code, res.text) res = res.json() first_musicc = res['data']['list'][0] name = first_musicc['name'] + '-' + first_musicc['artist'] rid = first_musicc['rid'] return name, rid def get_music_url(self, mid): url = 'http://bd.kuwo.cn/api/v1/www/music/playUrl' params = { 'mid': mid, 'type': 'convert_url3', 'httpsStatus': '1' } res = requests.get(url, params=params, headers=self.headers) print(res.status_code, res.text) mp3_url = res.json()['data']['url'] print('mp3_url', mp3_url) return mp3_url def download(self, name, url): res = requests.get(url, headers=self.headers) print(res.status_code) with open(f'{self.path}{name}.mp3', 'wb') as f: f.write(res.content) def main(self): while True: keyword = input('请输入歌曲名称(+歌手),输入T退出:') if keyword == 'T': break name, rid = self.search(keyword) url = self.get_music_url(rid) self.download(name, url) if __name__ == '__main__': k = KuwoMusic() k.main() # k.search('大鱼')
欢迎关注,爬虫王者
标签:__,name,url,res,self,音乐,headers,免费,下载 From: https://www.cnblogs.com/crawler-king/p/16993254.html