python播放在线音乐_Python实现在线音乐播放器
最近這幾天,學(xué)習(xí)了一下python,對(duì)于爬蟲比較感興趣,就做了一個(gè)簡(jiǎn)單的爬蟲項(xiàng)目,使用Python的庫(kù)Tkinsert做了一個(gè)界面,感覺(jué)這個(gè)庫(kù)使用起來(lái)還是挺方便的,音樂(lè)的數(shù)據(jù)來(lái)自網(wǎng)易云音樂(lè)的一個(gè)接口,通過(guò)requests模塊,get請(qǐng)求將數(shù)據(jù)獲得,使用Json模塊進(jìn)行數(shù)據(jù)的解析,最終使用python的mp3play庫(kù)進(jìn)行對(duì)音樂(lè)的在線播放,以下是該程序的源碼。
#!/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------來(lái)自網(wǎng)易云音樂(lè)-----')
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('',play)
listBox.pack()
root.mainloop()
def searchM():
global m_List
itemCount = 50
if not text.get():
tkMessageBox.showinfo('溫馨提示','您可以輸入以下內(nèi)容進(jìn)行搜索\n1.歌曲名\n2.歌手名\n3.部分歌詞')
return
#獲得輸入的歌名
url = 'http://s.music.163.com/search/get/?type=1&s=%s&limit=%s'%(text.get(),itemCount)
#get請(qǐng)求
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('溫馨提示','查詢過(guò)程出現(xiàn)錯(cuò)誤,請(qǐng)重試')
#print '查詢過(guò)程出現(xiàn)錯(cuò)誤,請(qǐng)重試'
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()
程序運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
本文標(biāo)題: Python實(shí)現(xiàn)在線音樂(lè)播放器
本文地址: http://www.cppcns.com/jiaoben/python/181012.html
總結(jié)
以上是生活随笔為你收集整理的python播放在线音乐_Python实现在线音乐播放器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 货拉拉app叫车方法详解 货拉拉app怎
- 下一篇: 阴阳师须佐之男技能是什么 阴阳师须佐之男