python:webbrowser 搜索云音乐

tk_yun_music.py

# -*- coding: utf-8 -*-
""" 搜索云音乐 """
import os
import json
import time
import tkinter as tk
#from tkinter import *
from tkinter import messagebox
from urllib import request, parse
import webbrowser as web

global x

def search():
    """ 搜索云音乐 """
    if not entry.get():
        messagebox.showinfo("温馨提示","搜索内容不能为空")
        return
    name = parse.quote(entry.get())
    print(name)
	# http://s.music.163.com/search/get/?type=1&s=%E7%88%B1&limit=9
    url = "http://s.music.163.com/search/get/?type=1&s=%s&limit=9"
    data = request.urlopen(url %name).read()
    js = json.loads(data)
    global x
    x = []
    listbox.delete(0, tk.END)
    for i in js['result']['songs']:
        listbox.insert(tk.END, '%s (%s)'%(i['name'],i['artists'][0]['name']))
        x.append(i['page'])
    file1 = "music.json"
    with open(file1, 'w') as fp:
        #fp.write(data) # TypeError: write() argument must be str, not bytes
        fp.write(json.dumps(js, sort_keys=True, indent=2)) # json pretty print

def play(self):
    index = listbox.curselection()
    var1.set(u"正在访问: "+ listbox.get(index,last=None))
    url = x[index[0]]
    print(url)
    web.open(url, new=0, autoraise=True)
    time.sleep(3)

# main()
root = tk.Tk()
root.title("搜索云音乐")
#root.geometry("640x480")
root.resizable(width=True, height=False)
# 输入框布局
frame1 = tk.Frame(root, width=50, height=30)
frame1.pack(side="top")
entry = tk.Entry(frame1, width=40)
entry.pack(side="left")
button = tk.Button(frame1, text="搜索云音乐", command=search)
button.pack(side="left", padx=5, pady=2)
# 退出按钮
quit_btn = tk.Button(frame1, text="退出", fg="red", command=root.quit)
quit_btn.pack(side="right", padx=5, pady=2)
# 显示栏
var1 = tk.StringVar()
label = tk.Label(root,textvariable=var1,fg="purple", font="Song 12")
label.pack()
# 列表框
listbox = tk.Listbox(root, width=50, font="Song 12", bg='yellow')
listbox.bind('<Double-Button-1>', play)
listbox.pack(side="bottom", fill='both', expand=True)

root.mainloop()

运行 tk_yun_music.py 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值