您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python制作线上音乐播放器的代码示例

2024/12/23 20:37:26发布13次查看
这篇文章主要为大家详细介绍了python实现在线音乐播放器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近这几天,学习了一下python,对于爬虫比较感兴趣,就做了一个简单的爬虫项目,使用python的库tkinsert做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音乐的一个接口,通过requests模块,get请求将数据获得,使用json模块进行数据的解析,最终使用python的mp3play库进行对音乐的在线播放,以下是该程序的源码。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @date : 2016-12-28 21:03:21 # @author : donoy (172829352@qq.com) # @link : http://www.cnblogs.com/donoy/ # @version : $id$ from tkinter import * import tkmessagebox import requests import json import urllib import mp3play import threading import time def center_window(root, width, height): screenwidth = root.winfo_screenwidth() screenheight = root.winfo_screenheight() size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2) root.geometry(size) def createwnd(): global root global listbox global text root = tk() root.title('-----dmplayer------来自网易云音乐-----') center_window(root, 440, 250) root['background'] = '#c7edcc' text = entry(font='宋体',width=36) text.pack() button = button(root,text='搜索',width=18,fg='red',background='#cdcdc1',command=searchm).pack() listbox = listbox(root, height=12,width=72,background='#c7edcc') listbox.bind('<double-button-1>',play) listbox.pack() root.mainloop() def searchm(): global m_list itemcount = 50 if not text.get(): tkmessagebox.showinfo('温馨提示','您可以输入以下内容进行搜索\n1.歌曲名\n2.歌手名\n3.部分歌词') return #获得输入的歌名 url = 'http://s.music.163.com/search/get/?type=1&s=%s&limit=%s'%(text.get(),itemcount) #get请求 header = {'user-agent':'mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/55.0.2883.75 safari/537.36'} html = requests.get(url,header) data = json.loads(html.text) m_list = [] try: listbox.delete(0,listbox.size()) for musicdata in data['result']['songs']: listbox.insert(end,musicdata['name'] +'------'+'(' +musicdata['artists'][0]['name'] + ')') m_list.append(musicdata['audio']) except exception as e: tkmessagebox.showinfo('温馨提示','查询过程出现错误,请重试') #print '查询过程出现错误,请重试' def play(args): try: global mp3 sy = listbox.curselection()[0] mp3 = mp3play.load(m_list[int(sy)]) mp3.play() #time.sleep(1000) except exception as e: pass def main(): createwnd() if name == 'main': main()
程序运行结果:
【相关推荐】
1. python免费视频教程
2. python面向对象视频教程
3. python基础教程
以上就是python制作线上音乐播放器的代码示例的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product