Python code example for creating an online music player

Source: Internet
Author: User
This article describes in detail how to use Python to implement online music players, interested friends can refer to this article for details about how to use Python to implement online music players. This article has some reference value. interested friends can refer to it.

In the past few days, I have learned python and I am interested in crawlers. I have made a simple crawler project and made an interface using the Python library Tkinsert, it seems that this library is quite convenient to use. The music data comes from an interface of NetEase Cloud Music. The data is obtained through the requests module and the get request. the Json module is used for data parsing, the mp3play library of python is used to play music online. The following is the source code of the program.

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Date: 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' % (width, height, (screenwidth-width)/2, (screenheight-height)/2) root. geometry (size) def createWnd (): global root global listBox global text root = Tk () root. title ('----- DMPlayer ------ from NetEase Cloud Music -----') center_window (root, 440,250) root ['background'] = '# C7EDCC' text = Entry (font = ' ', width = 36) text. pack () button = Button (root, text = 'search', width = 18, fg = 'red', background = '# cdc1', 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 ('Tips: ', you can enter the following content for search \ n1. song name \ n2. singer name \ n3. part of the lyrics') return # get the entered song name url =' http://s.music.163.com/search/get/?type=1&s=%s&limit=%s '% (Text. get (), itemCount) # get request 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']) cannot T Exception as e: tkMessageBox. showinfo ('warm hint ', 'query process error, please retry') # print 'query process error, please try 'Def play (args): try: global mp3 sy = listBox. curselection () [0] mp3 = mp3play. load (m_List [int (sy)]) mp3.play () # time. sleep (1000) failed t Exception as e: pass def main (): createWnd () if name = 'main': main ()
 

Program running result:

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.