Python code example for making a music player on line

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.