当前位置:   article > 正文

Python制作在线音乐播放器_csdn使用python web实现制作音乐按按钮

csdn使用python web实现制作音乐按按钮

思路:

1.使用Python爬虫对QQ音乐进行爬取数据,进行数据分析得到歌曲的url。
1.将搜索url进行拼接;
 https://y.qq.com/portal/search.html#page=1&searchid=1&remoteplace=txt.yqq.top&t=song&w={name}
2.根据搜索url进行爬取搜索列表
3.根据所选取的选项进行爬取,得到下载url:https://y.qq.com/n/yqq/song/002CD1h63aXAkm.html
4.通过接口网站进行爬取最终的音频文件的url
5.通过url下载到本地
6.设计GUI,实现音乐播放功能。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

效果:

在这里插入图片描述

代码:

from tkinter import *
from PIL import Image,ImageTk
from pymediainfo import MediaInfo
import IPython.display as ipd
from pygame import mixer
import threading
import pygame
import time
import requests
import json
import re
import os

def music(name):
    url = f"https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=62115878671550904&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w={name}&g_tk=5381&jsonpCallback=MusicJsonCallback2806001545440244&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0"
    json_list = requests.get(url).text
    res = re.compile(r"MusicJsonCallback.*?{(.*)}", re.S)
    content = re.findall(res, json_list)
    content_1 = '{'+content[0]+'}'
    dict_content = json.loads(content_1)
    keyword = dict_content["data"]["keyword"]
    list_all = dict_content["data"]["song"]["list"]
    mid_list = []
    music_title_list = []
    music_album_name = []
    music_singer_name = []
    for index, for_dict in enumerate(list_all):
        # 专辑名
        album_name = for_dict["album"]["title"]
        music_album_name.append(album_name)
        # new url
        mid_list.append(for_dict["mid"])
        # 歌名
        music_title = for_dict["title"]
        music_title_list.append(music_title)
        # 歌手
        singer_name = for_dict["singer"][0]["title"]
        music_singer_name.append(singer_name)
    return mid_list,music_title_list,music_singer_name,music_album_name

def get_url(data):
    url = 'http://www.douqq.com/qqmusic/qqapi.php' #接口网站
    headers = {
        "Content-Type":"application/json",
        'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
        'Host':'www.douqq.com',
        'Origin':'http://www.douqq.com',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
    }
    from_data = {
        'mid':data
    }
    response = requests.post(url=url,data=from_data,headers=headers)
    str_data = response.content.decode('utf-8')   #得到字符串数据
    str_data = json.loads(str_data)
    str_data = str_data.replace('\/\/','//').replace('\/','/')
    str_data = re.findall('"m4a":"(.*?)",',str_data,re.S)[0]
    return str_data
def save(my_str):
    url = my_str
    response = requests.get(url=url)
    result = response.content
    return result
def select(result):   #搜索按钮
    a = str(inp1.get())
    s = youdao(a)
    txt.delete(0.0, END)
    txt.insert(END, s)

def show():
    name = str(e1.get())#get是用来获取e1的内容的
    e2.delete(0.0, END)
    e2.insert(END,'   歌曲\t\t\t  歌手\t\t  专辑\n')
    global my_data
    my_data = music(name=name)
    for i in range(1,6):
        my_music_list = '%d  '%i+my_data[1][i]+'\t\t\t '+my_data[2][i]+'\t\t  '+my_data[3][i]+'\n'
        e2.insert(END, my_music_list)
def show1():
    name_num = str(e3.get())#get是用来获取e1的内容的
    e2.delete(0.0, END)
    e2.insert(END,'   歌曲\t\t\t  歌手\t\t  专辑\n')
    my_music_list = '   '+my_data[1][int(name_num)]+'\t\t\t '+my_data[2][int(name_num)]+'\t\t  '+my_data[3][int(name_num)]+'\n'
    e2.insert(END, my_music_list)
    data = 'https://y.qq.com/n/yqq/song/'+my_data[0][int(name_num)]+'.html'
    my_str = get_url(data)
    music = save(my_str)
    
    global my_music_name
    my_music_name = my_data[1][int(name_num)]
    with open('spyider_music\%s.mp3'%my_music_name,'wb') as f:
        f.write(music) 
        f.flush()
    time.sleep(5)
    mixer.init()
    mixer.music.load(r'spyider_music\\'+my_music_name+'.mp3')
    mixer.music.play()
    time.sleep(300)
    mixer.music.stop()
    e2.insert(END, '\n   歌曲缓存中,请等待...\n')
    e2.insert(END, '   缓存完毕!')

    
if __name__ == "__main__":
    app = Tk()
    app.geometry('540x280')
    app.title('音乐播放器')
    Label(app, text= "请输入歌名或者歌手名:").grid(row = 1, column = 0)
    theButton1 = Button(app, text = "搜索",background='#CA0316',foreground='white',activebackground='red',activeforeground='white',width = 10,command = show)
    theButton1.grid(row =1 ,column =4,sticky =W+E, padx=10,pady =5)
    v1 = StringVar()
    e1 = Entry(app, textvariable = v1)
    e2 = Text(app, font=('华文新魏',11))
    e1.grid(row = 1, column = 1)
    e2.place(relx=0.03,rely=0.14, relwidth=0.938,relheight=0.5,)
    Label(app, text= "   要听第几首歌(根据序号)?").grid(row = 6, column = 0)
    theButton2 = Button(app, text = "播放",background='#CA0316',foreground='white',activebackground='red',activeforeground='white',width = 10,command = show1)
    theButton2.grid(row =6 ,column =4,sticky =W+E, padx=10,pady =145)
    v3 = StringVar()
    e3 = Entry(app, textvariable = v3)    
    e3.grid(row = 6, column = 1)
 
    mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/112083
推荐阅读
相关标签
  

闽ICP备14008679号