赞
踩
本人python初学者,记录自己的一次学习过程,仅供参考。欢迎各位前辈指点。
本次要分析的页面主要是酷狗音乐的搜索界面:
思路如下:
def get_url(): k = time.time() k = int(round(k * 1000)) text = input("请输入搜索关键字") info = [ "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt", "appid=1014", "bitrate=0", "callback=callback123", "clienttime={}".format(k), "clientver=1000", "dfid=34d3K63Qc3AA3wvcEp1te6TT", "filter=10", "inputtype=0", "iscorrection=1", "isfuzzy=0", "keyword={}".format(text), "mid=41d5a0c36112c5cb310d75b0ab73ac0f", "page=1", "pagesize=30", "platform=WebFilter", "privilege_filter=0", "srcappid=2919", "token=", "userid=0", "uuid=41d5a0c36112c5cb310d75b0ab73ac0f", "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"] # 创建md5对象 new_md5 = md5() info = ''.join(info) # 更新哈希对象 new_md5.update(info.encode(encoding='utf-8')) # 加密 result = new_md5.hexdigest() music_info_url = 'https://complexsearch.kugou.com/v2/search/song?callback=callback123&srcappid=2919&clientver=1000&clienttime={}&' \ 'mid=41d5a0c36112c5cb310d75b0ab73ac0f&uuid=41d5a0c36112c5cb310d75b0ab73ac0f&dfid=34d3K63Qc3AA3wvcEp1te6TT&' \ 'keyword={}&page=1&pagesize=30&bitrate=0&isfuzzy=0&inputtype=0&platform=WebFilter&userid=0&iscorrection=1&' \ 'privilege_filter=0&filter=10&token=&appid=1014&signature={}'.format(k, text, result) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36', 'Cookie': '填入你自己的Cookie' } # 请求搜索界面的URL minfo_get = requests.get(music_info_url, headers=headers).text # 获得该页面的json格式并处理 minfo_get = minfo_get[12:-2] minfo = json.loads(minfo_get)['data']['lists'] num = json.loads(minfo_get)['data']['pagesize'] for j in range(0, num - 1): song_name = minfo[j]['SongName'] singer_name = minfo[j]['SingerName'] # dfid这个参数部分歌曲不能缺少,缺少则会出现出现无法正常获得url的情况。 # 但大多数歌曲都可以没有该参数。 # 如果有人知道原因的话,希望解答一下。感恩! music_url = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&dfid=34d3K63Qc3AA3wvcEp1te6TT&encode_album_audio_id={}'.format(minfo[j]['EMixSongID']) print(music_url) get_play_url = requests.get(music_url, headers=headers).text play_url = json.loads(get_play_url)['data']['play_url'] print(play_url)
该部分的代码非常简单。
代码如下:
# 选择位置
download_place = "pachong_music"
try:
with open('{}/{}---{}.mp3'.format(download_place, song_name, singer_name), 'wb') as f:
f.write(requests.get(play_url).content)
print("{}--{}保存成功".format(song_name, singer_name))
# 若无该歌曲资源
except requests.exceptions.MissingSchema as e:
print("{}--{}暂时不能播放".format(song_name, singer_name))
except KeyError as e:
print("{}--{}保存失败".format(song_name, singer_name))
f.close()
本文界面制作使用的是tkinter库。由于python水平还比较差,所以功能非常简单。
只有搜索并显示音乐信息。
浏览本地文件夹,选择位置。
希望以后学习,能够完善更多功能。
点击按钮后,改变相应的变量值选择方法是使用全局变量。下面直接放代码吧。
滚动窗口使用的是tkinter中的scrolledtext
浏览使用的是tkinter中的filedialog
import json import time from hashlib import md5 import requests import tkinter as tk from tkinter import scrolledtext from tkinter import filedialog def get_url(): global music_info_url text = key_word.get() # TODO:自定义关键词搜索页面的URL # js逆向部分参考:https://www.cnblogs.com/wxd501/p/17071045.html k = time.time() k = int(round(k * 1000)) info = [ "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt", "appid=1014", "bitrate=0", "callback=callback123", "clienttime={}".format(k), "clientver=1000", "dfid=34d3K63Qc3AA3wvcEp1te6TT", "filter=10", "inputtype=0", "iscorrection=1", "isfuzzy=0", "keyword={}".format(text), "mid=41d5a0c36112c5cb310d75b0ab73ac0f", "page=1", "pagesize=30", "platform=WebFilter", "privilege_filter=0", "srcappid=2919", "token=", "userid=0", "uuid=41d5a0c36112c5cb310d75b0ab73ac0f", "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"] # 创建md5对象 new_md5 = md5() info = ''.join(info) # 更新哈希对象 new_md5.update(info.encode(encoding='utf-8')) # 加密 result = new_md5.hexdigest() music_info_url = 'https://complexsearch.kugou.com/v2/search/song?callback=callback123&srcappid=2919&clientver=1000&clienttime={}&' \ 'mid=41d5a0c36112c5cb310d75b0ab73ac0f&uuid=41d5a0c36112c5cb310d75b0ab73ac0f&dfid=34d3K63Qc3AA3wvcEp1te6TT&' \ 'keyword={}&page=1&pagesize=30&bitrate=0&isfuzzy=0&inputtype=0&platform=WebFilter&userid=0&iscorrection=1&' \ 'privilege_filter=0&filter=10&token=&appid=1014&signature={}'.format(k, text, result) # con_info.insert("end", music_info_url) # con_info.insert("end", '\n') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36', 'Cookie': '****' } minfo_get = requests.get(music_info_url, headers=headers).text minfo_get = minfo_get[12:-2] minfo = json.loads(minfo_get)['data']['lists'] # for i, k in enumerate(minfo): # print(i, k['SingerName'], k['EMixSongID']) # num为页码包含歌曲数量 num = json.loads(minfo_get)['data']['pagesize'] con_info.insert("end", '搜索到的歌曲如下:\n') for j in range(0, num - 1): song_name = minfo[j]['SongName'] singer_name = minfo[j]['SingerName'] music_url = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&dfid=34d3K63Qc3AA3wvcEp1te6TT&encode_album_audio_id={}'.format(minfo[j]['EMixSongID']) get_play_url = requests.get(music_url, headers=headers).text json.loads(get_play_url)['data']['play_url'] con_info.insert("end", "{}--{}".format(song_name, singer_name)) con_info.insert("end", '\n') # TODO:浏览文件夹,选择本地位置 def browse(): global download_place download_place = filedialog.askdirectory() con_info.insert("end", "即将保存的路径为{}".format(download_place)) con_info.insert("end", '\n') # TODO:获取并写入文件 def download_music(): global music_info_url headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36', 'Cookie': 'kg_mid=41d5a0c36112c5cb310d75b0ab73ac0f; kg_dfid=34d3K63Qc3AA3wvcEp1te6TT; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1677400096; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1677411756' } minfo_get = requests.get(music_info_url, headers=headers).text minfo_get = minfo_get[12:-2] minfo = json.loads(minfo_get)['data']['lists'] # num为页码包含歌曲数量 num = json.loads(minfo_get)['data']['pagesize'] for j in range(0, num - 1): song_name = minfo[j]['SongName'] singer_name = minfo[j]['SingerName'] music_url = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&encode_album_audio_id={}'.format( minfo[j]['EMixSongID']) get_play_url = requests.get(music_url, headers=headers).text play_url = json.loads(get_play_url)['data']['play_url'] try: with open('{}/{}---{}.mp3'.format(download_place, song_name, singer_name), 'wb') as f: f.write(requests.get(play_url).content) except requests.exceptions.MissingSchema as e: con_info.insert("未获得歌曲{}--{}资源".format(song_name, singer_name)) except KeyError as e: con_info.insert("{}--{}保存失败".format(song_name, singer_name)) f.close() con_info.insert("end", "{}--{}保存成功".format(song_name, singer_name)) con_info.insert("end", '\n') if __name__ == '__main__': music_info_url = "" download_place = "" # TODO:UI界面 root = tk.Tk() root.title("music") root.geometry('1024x512') # 添加搜索关键词框 key_word = tk.Entry(root) key_word.pack() # 点击搜索button执行get_url函数,获得连接 find_button = tk.Button(root, text="搜索", command=get_url) find_button.pack() # 点击浏览button执行browse函数,选择保存文件存储位置 browse_button = tk.Button(root, text="浏览", command=browse) browse_button.pack() # 点击保存button执行download_music函数,保存歌曲 down_button = tk.Button(root, text="保存", command=download_music) down_button.pack() # 设置一个输出控制台信息的滚动文本框 frame2 = tk.LabelFrame(root, text="控制台信息") frame2.pack() con_info = scrolledtext.ScrolledText(frame2) con_info.pack() root.mainloop()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。