我们听歌什么的,现在这限制那限制,要不只能在这个平台 ,要不就只能在那个平台,多麻烦,咱们直接用代码全部下载下来用本地播放器播放,美滋滋!
这里咱们用python和pycharm就好了,没有安装的小伙伴可以先安装一下。
这两个模块也需要安装一下
requests
prettytable
打开cmd输入(pip install 加上模块名)回车等待即可,第二个也一样的。
本次主要实现
搜索功能
下载歌曲功能
import requests # 发送网络请求
import json
import prettytable as pt
headers = {
'cookie': 'pgv_pvid=7300130020; tvfe_boss_uuid=242c5295a1cb156d; RK=6izJ0rkfNn; ptcz=622f5bd082de70e3e6e9a077923b48f72600cafd5e4b1e585e5f418570fa30fe; ptui_loginuin=1321228067; luin=o3452264669; o_cookie=3452264669; ts_uid=5501087131; fqm_pvqid=89ea2cc7-6806-4091-989f-5bc2f2cdea5c; lskey=00010000d96bdb23303bc141246a5c2f9a02cf45acf079eaab6645fdc10923e7a0eaac09aa24533ab1299555; fqm_sessionid=d77623f3-4dd0-4709-8e50-a34986b17344; pgv_info=ssid=s4530794666; ts_last=y.qq.com/; ts_refer=ADTAGmyqq; _qpsvr_localtk=0.8458135546904957; euin=oK6kowEAoK4z7eclow6qoiSz7z**; psrf_qqrefresh_token=; psrf_qqaccess_token=; wxuin=1152921504872193707; psrf_qqopenid=; wxrefresh_token=53_FHlMDgGCCfOUCXRxCMGFGYHxwtDV0_rZANu84l_KrYtPYRVSWqpxoWqojdj1coj4CF_H4qNrXw0WrZtQdlsozcwYQbVp0SepI9Jmm_Pfhjk; qqmusic_key=W_X_3mJrl760n0lDWkgqqBhBMX2VPnH1PNwwUZjvNkmSnurNjTTap1J6gjetIU50hraK3AjHgw5T4ay2R3; qm_keyst=W_X_3mJrl760n0lDWkgqqBhBMX2VPnH1PNwwUZjvNkmSnurNjTTap1J6gjetIU50hraK3AjHgw5T4ay2R3; psrf_qqunionid=; tmeLoginType=1; wxunionid=oqFLxsgDOwYRnPDffuK5UINv958c; wxopenid=opCFJw5Vvcf24VLQSyNpKAX6gt3I; wxuin=1152921504872193707; qm_keyst=W_X_3mJrl760n0lDWkgqqBhBMX2VPnH1PNwwUZjvNkmSnurNjTTap1J6gjetIU50hraK3AjHgw5T4ay2R3; login_type=2'
}
name = input('请输入你想要下载的歌曲或者歌手名称:')
# 1. 发送请求 向以前的搜索功能接口发送 请求
url = f'https://c.***/soso/fcgi-bin/client_search_cp?p=1&n=10&w={name}'
response = requests.get(url)
# print(response)
# 2. 获取数据 获取所有歌曲信息数据
json_str = response.text
# 3. 解析数据 歌曲 歌手名 专辑 歌曲mid(用来下载歌曲必须要的参数)
json_str = json_str[9: -1]
json_dict = json.loads(json_str)
song_list = json_dict['data']['song']['list']
tb = pt.PrettyTable()
tb.field_names = ['序号', '歌名', '歌手', '专辑']
music_info_list = []
count = 0
# 4. 格式化输出
for song in song_list:
songname = song['songname']
songmid = song['songmid']
singer = song['singer'][0]['name']
albumname = song['albumname']
tb.add_row([count, songname, singer, albumname])
music_info_list.append([songmid, songname, singer])
count += 1
print(tb)
while True:
input_index = eval(input('请输入你要下载的歌曲序号(-1)退出:'))
if input_index == -1:
break
download_info = music_info_list[input_index]
songmid = download_info[0]
# 1. 通过获取的歌曲mid 拼接 需要的音乐url
music_info_url = 'https://u.***/cgi-bin/musicu.fcg?data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch", "filename":"M800","param":{"guid":"8846039534","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","filename":"M800","param":{"guid":"8846039534","songmid":["%s"],"songtype":[0],"uin":"1152921504784213523","loginflag":1,"platform":"20"}},"comm":{"uin":"1152921504784213523","format":"json","ct":24,"cv":0}}' % songmid
# print(music_info_url)
# 2. 发送网络请求 需要的音乐url
json_data = requests.get(url=music_info_url, headers=headers).json()
# 3. 获取数据 获取 里面生成的 部分音乐链接 合并 (mp3 数据所在的链接了)
purl = json_data['req_0']['data']['midurlinfo'][0]['purl']
media_url = 'https://dl.stream.qqmusic.qq.com/' + purl
# 4. 发送请求 (mp3 数据所在的链接了)
# 5. 获取数据 音乐二进制数据
music_data = requests.get(media_url).content
# 6. 保存数据
with open(f'歌曲下载/{download_info[1]}-{download_info[2]}.mp3', mode='wb') as f:
f.write(music_data)
print(f'{download_info[1]}, 下载完成!')
兄弟们,快去试试吧~
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/fei347795790/article/details/123237273
内容来源于网络,如有侵权,请联系作者删除!