请将以下代码改为pygame由支持 代码如下print潘昱霖!!!!!!!!!!!!!# 导入模块from tkinter import import requestsimport jsonpathimport osfrom urllibrequest import urlretrieve# 2功能实现 1url 2模拟浏览器请求 3解析网页源代码 4保存数据def son
导入模块
import pygame import requests import jsonpath import os from urllib.request import urlretrieve
初始化pygame
pygame.init()
2.功能实现
""" 1.url 2.模拟浏览器请求 3.解析网页源代码 4.保存数据 """ def song_download(url,title,author): # 创建文件夹 os.makedirs("music",exist_ok=True) path = 'music{}.mp3'.format(title) text.insert(END,'歌曲:{0}-{1},正在下载...'.format(title,author)) # 文本框滑动 text.see(END) # 更新 text.update() # 下载 urlretrieve(url,path) text.insert(END,'下载完毕,{0}-{1},请试听'.format(title,author)) # 文本框滑动 text.see(END) # 更新 text.update()
def get_music_name(): """ 搜索歌曲名称 :return: """ name = entry.get() platfrom = var.get() # name = '白月光与朱砂痣' url = 'https://music.liuzhijin.cn/' headers = { "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36", # 判断请求是异步还是同步 "x-requested-with":"XMLHttpRequest" } param = { "input":name, "filter":"name", "type":platfrom, "page": 1, } res = requests.post(url=url,data=param,headers=headers) json_text = res.json()
title = jsonpath.jsonpath(json_text,'$..title')
author = jsonpath.jsonpath(json_text,'$..author')
url = jsonpath.jsonpath(json_text, '$..url')
print(title,author,url)
song_download(url[0],title[0],author[0])
1.用户界面
创建画板
root = pygame.display.set_mode((560, 450))
设置窗口标题
pygame.display.set_caption('全网音乐下载器')
设置窗口出现的位置
pygame.display.set_mode((400, 200))
标签组件
label = pygame.font.SysFont('楷体', 20) label_surface = label.render('请输入下载的歌曲:', True, (0, 0, 0)) root.blit(label_surface, (0, 0))
输入框组件
entry = pygame.font.SysFont('宋体', 20) entry_surface = entry.render('', True, (0, 0, 0)) root.blit(entry_surface, (0, 30))
单选按钮
var = StringVar() r1 = pygame.font.SysFont('楷体', 20) r1_surface = r1.render('网易云', True, (0, 0, 0)) root.blit(r1_surface, (0, 60)) r2 = pygame.font.SysFont('楷体', 20) r2_surface = r2.render('QQ', True, (0, 0, 0)) root.blit(r2_surface, (100, 60))
列表框
text = pygame.font.SysFont('楷体', 16) text_surface = text.render('', True, (0, 0, 0)) root.blit(text_surface, (0, 90))
下载按钮
button1 = pygame.font.SysFont('楷体', 20) button1_surface = button1.render('', True, (0, 0, 0)) root.blit(button1_surface, (0, 300))
刷新屏幕
pygame.display.flip(
原文地址: https://www.cveoy.top/t/topic/g2gF 著作权归作者所有。请勿转载和采集!