音乐下载器 初版 未做异常处理
本帖最后由 xiaqiusheng 于 2023-7-28 09:53 编辑import json
import os
from tkinter import *
from urllib.request import urlretrieve
import requests
from urllib.parse import quote
```
def song_load(music_name, music_url):
# 创建文件夹
os.makedirs('downloaded_music', exist_ok=True)
path = 'downloaded_music\{}.mp4'.format(music_name)
text.insert(END, '文件:{},正在下载...'.format(music_name))
# 文本框滚动
text.see(END)
# 更新
text.update()
urlretrieve(music_url, path)
text.insert(END, '下载完毕:{}'.format(music_name))
# 文本框滚动
text.see(END)
# 更新
text.update()
```
```
def huoqu_music():
try:
text.delete(0, END)
music_info = []# 存储已获取的音乐ID
for i in range(0, 55):
key_word = quote(entry.get())
url = f'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={key_word}&pn={i}&rn=20&httpsStatus=1&reqId=273867a0-2c5e-11ee-b1b5-db033bb09d98&plat=web_www&from='
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Cookie": "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1690445552; _ga=GA1.2.492811842.1690445552; _gid=GA1.2.1407149096.1690445552; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1690449315; Hm_Iuvt_cdb524f42f0ce19b169b8072123a4727=ATRHZ5WEnw4jJMmQwYEzJcr8QKCPBXjE; _ga_ETPBRPM9ML=GS1.2.1690449316.2.1.1690449339.37.0.0",
"Pragma": "no-cache",
"Referer": "http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6",
"Secret": "1f082824a0b1ba4cd04cd92aeeabf4ac4aecbcf2f78e4eb329374f81e6e17d7e004c83b3",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Accept-Encoding": "deflate, gzip"
}
res = requests.get(url, headers=headers).text
dic = json.loads(res)
for item in dic["data"]["list"]:
if item["isListenFee"] == False:
music_name = item["name"]
music_id = item["rid"]
music_art = item["artist"]
music_free = item["isListenFee"]
# 过滤重复的music_id
if music_id not in {m['music_id'] for m in music_info}:
music_info.append({'music_name': music_name, 'music_id': music_id})
# 成功获取音乐信息后,调用下载音乐的函数
down_music(music_info)
except requests.exceptions.RequestException as e:
text.insert(END, f'API 请求发生错误: {e}')
except json.JSONDecodeError as e:
text.insert(END, f'JSON 解析错误: {e}')
```
```
def down_music(music_info):
os.makedirs('downloaded_music', exist_ok=True)
for music in music_info:
music_id = music['music_id']
music_name = music['music_name']
url = f"http://www.kuwo.cn/api/v1/www/music/playUrl?mid={music_id}&type=music&httpsStatus=1&reqId=c46eeee0-2c59-11ee-aecd-8d89c636063c&plat=web_www&from="
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Cookie": "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1690445552; _ga=GA1.2.492811842.1690445552; _gid=GA1.2.1407149096.1690445552; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1690447147; _ga_ETPBRPM9ML=GS1.2.1690445552.1.1.1690447148.60.0.0; Hm_Iuvt_cdb524f42f0ce19b169b8072123a4727=jD6p4jscTSwTH4dRmRYQ68YtPyPHWmMm",
"Pragma": "no-cache",
"Referer": "http://www.kuwo.cn/search/list?key=^%^E5^%^BC^%^A0^%^E9^%^9F^%^B6^%^E6^%^B6^%^B5",
"Secret": "34184c1cceee9e6aea689a14ecd2fdaf50e7a0d98bd565ff28055c99f3d45a5602562d24",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Accept-Encoding": "deflate, gzip"
}
res = requests.get(url, headers=headers).json()
music_url = res['data']['url']
#print(music_name,music_url)
# 下载
music = requests.get(music_url, headers=headers).content
with open(f"downloaded_music/{music_name}.mp4", mode="wb") as f:
song_load(music_name, music_url)
f.write(music)
#print(f"下载完成:{music_name}")
```
```
root = Tk()
# 2.添加标题
root.title('音乐下载器')
# 3.设置窗口大小
root.geometry('560x450+400+200')
# 4.标签控件
label = Label(root, text='请输入歌手:', font=('华文行楷', 20))
# 5.定位
label.grid()
# 6.输入框
entry = Entry(root, font=('隶书', 20))
# 7.定位
entry.grid(row=0, column=1)
# 8.列表框
text = Listbox(root, font=('楷书', 16), width=50, heigh=15)
# 9.定位 columnspan 组件横跨的列数
text.grid(row=3, columnspan=2)
# 点击下载按钮
button = Button(root, text='开始下载', font=('隶书', 15), command=huoqu_music)
# 定位 sticky 对齐方式 W E N S东南西北
button.grid(row=4, column=0, sticky=W)
# 退出程序的按钮
button1 = Button(root, text='退出程序', font=('隶书', 15), command=root.quit)
# 定位 sticky 对齐方式 W E N S东南西北
button1.grid(row=4, column=1, sticky=E)
# 显示界面
root.mainloop()
``` xiaqiusheng 发表于 2023-7-28 17:27
需要会看一些报文,返回的内容有你想要的就是目标url
所以你是直接抓的软件啊?{"code":-1,"msg":"该歌曲为付费内容,请下载酷我音乐客户端后付费收听","reqId":"7de4aec866ea5497de7353ec07664859","profileId":"site","curTime":1690541965930,"success":false}我碰到了这个 lookfeiji 发表于 2023-7-28 19:02
所以你是直接抓的软件啊?{"code":-1,"msg":"该歌曲为付费内容,请下载酷我音乐客户端后付费收听","reqId ...
楼主不是直接抓的软件,程序中填写的是他自己账号的cookie, 他的账号没有会员,所以这个软件请求酷我服务器下载需要vip才能下的歌时就会返回这个报错。如果你有酷我音乐的会员可以在代码里替换掉楼主的cookie就可以下载需要vip才能下载的歌曲了。 虽然看不懂,但是还是觉得很厉害的样子,哈哈哈哈,搜索内容是直接在命令行里改? ww7933921 发表于 2023-7-28 14:08
虽然看不懂,但是还是觉得很厉害的样子,哈哈哈哈,搜索内容是直接在命令行里改?
可惜作者不敢发成品,怕违规 ww7933921 发表于 2023-7-28 14:08
虽然看不懂,但是还是觉得很厉害的样子,哈哈哈哈,搜索内容是直接在命令行里改?
在运行后的窗口页面输入就行 Marken888 发表于 2023-7-28 14:15
可惜作者不敢发成品,怕违规
可以使用,只是其余些歌曲下载失败未作处理 弱弱的问一句,下载url怎么抓出来的 感谢分享,如果有成品就更加好了 lookfeiji 发表于 2023-7-28 14:38
弱弱的问一句,下载url怎么抓出来的
需要会看一些报文,返回的内容有你想要的就是目标url 梁茵 发表于 2023-7-28 14:50
感谢分享,如果有成品就更加好了
异常处理就自己diy就好拉 xiaqiusheng 发表于 2023-7-28 17:28
异常处理就自己diy就好拉
好的,感谢楼主分享知识:lol