说明:
1.调用咪咕音乐api进行音乐下载(暂不支持批量)
2.使用自行安装import的库
效果:
源码:
[Python] 纯文本查看 复制代码
import requestsimport urllib.parsefrom tqdm import tqdmimport timedef get_songs(url,keyword): #对keyword进行urlencode加密 key = urllib.parse.quote(keyword) headers={ 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36' } #row为最大显示的结果数 params = { 'rows': 20, 'type': 2, 'keyword': key, 'pgc': 1, } response = requests.get(url,headers=headers,params=params) if response.status_code ==200: return response.json()['musics'] else: print('fail to get audiolist. status_code is',response.status_code)#下载进度可视化def downloadFILE(url,name): resp = requests.get(url=url,stream=True) content_size = int(int(resp.headers['Content-Length'])/1024) with open(name, "wb") as f: print("Pkg total size is:",content_size,'k,start...') for data in tqdm(iterable=resp.iter_content(1024),total=content_size,unit='k',desc=name): f.write(data) print(name , "download finished!")def main(): url = 'http://m.music.migu.cn/migu/remoting/scr_search_tag' keyword = input('其输入搜索关键词:') audiolist = get_songs(url,keyword) count = 0 for item in audiolist: songName = item['songName'] artist = item['artist'] #mp3 = item['mp3'] print(count,songName,artist) count+=1 index = input('请输入需要下载的歌曲前序号(默认下载第一首):') if index == '': index = 0 target_link = audiolist[int(index)]['mp3'] save_name = audiolist[int(index)]['songName']+' '+audiolist[int(index)]['artist']+'.mp3' downloadFILE(target_link,save_name) if __name__ == "__main__": main()
总结
1.咪咕的这个api基本没有什么防护只有keyword进行了urlencode,headers中习惯性加了use-agent其他都不需要就可以返回数据
2.params中的row是返回的最大结果数
py文件和exe文件的下载地址:
https://www.lanzous.com/i6j4nob
声明:本站所有资源均由网友分享,如有侵权内容,请在文章下方留言,本站会立即处理。