This article mainly for you to introduce the Python implementation of online music player related data, with a certain reference value, interested in small partners can refer to
Recently these days, learning a bit of Python, for the crawler is more interested in doing a simple crawler project, using Python library Tkinsert made an interface, feel that the library is very convenient to use, music data from the NetEase Cloud music interface, Through the requests module, GET request to obtain data, using JSON module for data parsing, and finally use the Python Mp3play library for music online playback, the following is the program's source code.
#!/usr/bin/env python#-*-coding:utf-8-*-# @Date: 2016-12-28 21:03:21# @Author: Donoy (172829352@qq.com) # @Link: htt p://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.t Itle ('-----dmplayer------from NetEase Cloud Music-----') Center_window (root, root[' background ') = ' #C7EDCC ' text = Entry (font= ' Song body ', width=36) text.pack () button = button (root,text= ' Search ', 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 = If not Text.get (): Tkmessagebox.showinfo (' Warm tip ', ' You can enter the following to search \n1. Song name \n2. Singer name \n3. Part lyrics ') return #获得输 Enter the song name 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 = j Son.loads (html.text) m_list = [] Try:listBox.delete (0,listbox.size ()) for musicdata in data[' result ' [' Songs ']: Listb Ox.insert (end,musicdata[' name ' + '------' + ' (' +musicdata[' artists '][0][' name '] + ') ') m_list.append (musicdata[') Audio ']) except Exception as E:tkmessagebox.showinfo (' Warm tip ', ' Error in query process, Retry ') #print ' error in the query process, please retry ' def Play ' (args): try : Global mp3 sy = listbox.curselection () [0] mp3 = mp3play.load (M_list[int (SY)]) Mp3.play () #time. Sleep (+) except E Xception as E:pass def Main (): Createwnd () if name = = ' Main ': Main ()
Program Run Result:
"Recommended"
1. Python Free video tutorial
2. Python Object-oriented video tutorial
3. Basic Python Tutorial