Python實現線上音樂播放器,python音樂播放器

來源:互聯網
上載者:User

Python實現線上音樂播放器,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 tkMessageBoximport requestsimport jsonimport urllibimport mp3playimport threadingimport timedef 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()

程式運行結果:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.