最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)音樂播放和下載小程序功能

 更新時(shí)間:2020年04月26日 10:07:44   作者:粽子哥哥123  
這篇文章主要介紹了python實(shí)現(xiàn)音樂播放和下載小程序功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

(本篇部分代碼綜合整理自B站,B站有手把手說明的教程)

1.網(wǎng)易云非付費(fèi)內(nèi)容爬取器(聲明:由于技術(shù)十分簡單,未到觸犯軟件使用規(guī)則的程度)驅(qū)動(dòng)Edge瀏覽器(自己寫驅(qū)動(dòng)會(huì)更高端)進(jìn)入界面,爬取列表中第一個(gè)音頻地址并存入相應(yīng)文件夾中。這里有一個(gè)最簡單的爬蟲程序和一個(gè)最簡單的tkinter GUI編程。

注意,要先在網(wǎng)易云音樂網(wǎng)頁中將第一個(gè)對(duì)應(yīng)音頻鏈接的位置定位:

在這里插入圖片描述

對(duì)于以上定位可通過如下方式獲得(定位器):

 req = driver.find_element_by_id('m-search')
 a_id = req.find_element_by_xpath('.//div[@class = "item f-cb h-flag "]/div[2]//a').get_attribute("href")

在XML語言中尋找鏈接路徑的方法可參見find_element_by_xpath
創(chuàng)建目錄參見makedirs

這里的GUI需要tkinter添加文本。用text控件insert(插入文本)、see(滾動(dòng))、update(更新)等方法顯示正在下載和已下載圖樣;在get_music_name函數(shù)中,首先從輸入窗口獲取名稱,然后調(diào)用Edge驅(qū)動(dòng)訪問網(wǎng)易云音樂主頁,通過'http://music.163.com/song/media/outer/url?id={}.mp3'.format(song_id)搜到歌曲,通過上述定位器找到歌曲地址和歌名。注意到第一個(gè)函數(shù)傳入的應(yīng)該是字典類型(有了這種語句:song_id = item['song_id']),那就創(chuàng)建一個(gè)字典后在函數(shù)體內(nèi)調(diào)用song_load實(shí)現(xiàn)下載。在這之前,驅(qū)動(dòng)就完成了任務(wù),所以可以關(guān)閉驅(qū)動(dòng)。
至于Tkinter的控件內(nèi)容,應(yīng)該根據(jù)實(shí)際情況試錯(cuò)和設(shè)計(jì),界面編程相對(duì)還是比較簡單的。(分別創(chuàng)建標(biāo)簽控件、輸入框、列表框、按鈕,并依次確定它們?cè)谥鹘缑嬷械奈恢茫?/p>

from tkinter import *
from selenium import webdriver
global entry
import os
from urllib.request import urlretrieve
#2.下載歌曲
def song_load(item):


 song_id = item['song_id']
 song_name = item['song_name']

 song_url = 'http://music.163.com/song/media/outer/url?id={}.mp3'.format(song_id)

 #創(chuàng)建文件夾
 os.makedirs('music_netease',exist_ok=True)
 path = 'music_netease\{}.mp3'.format(song_name)

 #顯示數(shù)據(jù)到文本框
 text.insert(END,'歌曲:{},正在下載...'.format(song_name))
 #文本框滾動(dòng)
 text.see(END)
 #更新
 text.update()
 #下載
 urlretrieve(song_url,path)
 #顯示數(shù)據(jù)到文本框
 text.insert(END,'歌曲:{},下載完畢'.format(song_name))
 #文本框滾動(dòng)
 text.see(END)
 #更新
 text.update()
#1.搜索
def get_music_name():
 #獲取輸入的內(nèi)容
 name = entry.get()

 url = 'https://music.163.com/#/search/m/?s={}&type=1'.format(name)
 #搜索頁面

 option = webdriver.EdgeOptions()
 option.add_argument('--headless')
 #driver = webdriver.Edge(edge_options=option)

 driver = webdriver.Edge('D:\\python\\msedgedriver')

 driver.get(url)
 driver.switch_to.frame('g_iframe')
 #獲取歌曲的id
 req = driver.find_element_by_id('m-search')
 a_id = req.find_element_by_xpath('.//div[@class = "item f-cb h-flag "]/div[2]//a').get_attribute("href")

 song_id = a_id.split('=')[-1]
 print(song_id)

 song_name = req.find_element_by_xpath('.//div[@class="item f-cb h-flag "]/div[2]//b').get_attribute("title")
 print(song_name)
 #構(gòu)造字典
 item = {}
 item['song_id'] = song_id
 item['song_name'] = song_name
 driver.quit()
 #下載歌曲
 song_load(item)

#get_music_name()


#形象工程
# 搭建界面
#創(chuàng)建畫板
root = Tk()

#標(biāo)題
root.title('網(wǎng)易云下載器')
#設(shè)置窗口大小
root.geometry('560x450+400+200')
#標(biāo)簽控件
label = Label(root,text = '輸入要下載的歌曲:',font = ('華文行楷',20))
#標(biāo)簽定位
label.grid()
#輸入框
entry = Entry(root,font = ('楷書',20))
#定位
entry.grid(row = 0,column = 1)
#列表框
text = Listbox(root,font = ('隸書',16),width = 50, heigh = 15)
text.grid(row = 1,columnspan = 2)
#點(diǎn)擊按鈕
button = Button(root,text = '開始下載',font = ('楷書',15),command=get_music_name)
button.grid(row=2, column=0,sticky=W)

button1 = Button(root,text = '退出程序',font = ('楷書',15),command=root.quit)
button1.grid(row=2, column=1,sticky=E)
#顯示當(dāng)前的界面內(nèi)容
root.mainloop()

運(yùn)行效果

在這里插入圖片描述

發(fā)現(xiàn)music_netease文件夾中相關(guān)文件赫然在列。

在這里插入圖片描述

簡易音樂播放器:
這個(gè)控件在界面上仍然使用Tkinter,只不過沒有通過編程,而是利用pygame庫中的音頻模塊,在邏輯上增加了上一曲、下一曲(讀取上一個(gè)文件、下一個(gè)文件)、音量控制、簡單的線程控制等。

import os
import tkinter
import tkinter.filedialog
import time
import threading
import pygame

#第一步 搭建界面
root = tkinter.Tk()
root.title('音樂播放器')
#窗口大小和位置
root.geometry('460x600+500+100')
#不能拉伸
root.resizable(False,False)

folder = ''#文件路徑
res = []
num = 0
now_music = ''
#第二步 功能實(shí)現(xiàn)
def buttonChooseClik():
 '''添加文件函數(shù)'''
 global folder
 global res
 if not folder:
  folder = tkinter.filedialog.askdirectory()#選擇目錄
  musics = [folder + '\\' + music
     for music in os.listdir(folder) if music.endswith(('.mp3','ogg'))]
  ret = []
  for i in musics:
   ret.append(i.split('\\')[1:])
   res.append(i.replace('\\','/'))
  var2 = tkinter.StringVar()
  var2.set(ret)
  #放入列表框
  lb = tkinter.Listbox(root,listvariable =var2)
  lb.place(x=50,y=100,width=260,height=300)
 if not folder:
  return
 global playing
 playing = True

 # 根據(jù)情況禁用和啟用相應(yīng)的按鈕
 buttonPlay['state'] = 'normal'
 buttonStop['state'] = 'normal'
 pause_resume.set('播放')


def play():
 '''播放音樂的函數(shù)'''
 #初始化混音器設(shè)備
 if len(res):
  pygame.mixer.init()
  global num
  while playing:
   if not pygame.mixer.music.get_busy():
    #隨機(jī)播放一首歌曲
    nextMusic =res[num]
    print(nextMusic)
    print(num)
    pygame.mixer.music.load(nextMusic.encode())
    #播放一次
    pygame.mixer.music.play(1)
    #print(len(res)-1)
    if len(res) -1==num:
     num=0
    else:
     num = num+1
    nextMusic = nextMusic.split('\\')[1:]
    musicName.set('playing....'+''.join(nextMusic))
   else:
    time.sleep(0.1)

def bottonPlayClik():
 '''點(diǎn)擊播放'''
 buttonNext['state'] = 'normal'
 buttonPrev['state'] = 'normal'

 if pause_resume.get() == '播放':
  pause_resume.set('暫停')
  global folder
  if not folder:
   #選擇目錄,返回目錄名
   folder = tkinter.filedialog.askdirectory()
  if not folder:
   return
  global playing
  playing = True

  #創(chuàng)建線程,主線程接受用戶操作

  t = threading.Thread(target=play)
  t.start()
 elif pause_resume.get() == '暫停':
  pygame.mixer.music.pause()
  pause_resume.set('繼續(xù)')
 elif pause_resume.get() == '繼續(xù)':
  pygame.mixer.music.unpause()
  pause_resume.set('暫停')


def bottonStopClik():
 '''停止播放'''
 global playing
 playing = False
 pygame.mixer.music.stop()
def bottonNextClik():
 '''播放下一首'''
 global playing
 playing = False
 pygame.mixer.music.stop()
 global num
 if len(res)== num:
  num = 0
 playing = True

 t = threading.Thread(target=play)
 t.start()

def bottonPrevClik():
 '''播放上一首'''
 global playing
 playing = True
 pygame.mixer.music.stop()
 global num
 if num == 0:
  num = len(res)-2
 elif num == len(res) - 1:
  num -= 2
 else:
  num -=2
 print(num)

 playing = True

 t = threading.Thread(target = play)

 t.start()

def closeWindow():
 '''關(guān)閉窗口'''
 global playing
 playing = False
 time.sleep(0.3)
 try:
  pygame.mixer.music.stop()
  pygame.mixer.quit()
 except:
  pass
 root.destroy()

def control_voice(value = 0.5):
 pygame.mixer.music.set_volume(float(value))

#關(guān)閉窗口
root.protocol('WM_DELETE_WINDOW',closeWindow)
#添加按鈕
buttonChoose = tkinter.Button(root,text='添加',command=buttonChooseClik)
#布局
buttonChoose.place(x=50,y=10,width=50,height=20)
#播放按鈕 跟蹤變量值的變化
pause_resume = tkinter.StringVar(root,value='播放')
buttonPlay= tkinter.Button(root,textvariable=pause_resume,command=bottonPlayClik)
buttonPlay.place(x=190,y=10,width=50,height=20)
buttonPlay['state'] = 'disabled'
#停止播放
buttonStop = tkinter.Button(root,text = '停止',command=bottonStopClik)
#布局
buttonStop.place(x=120,y=10,width=50,height=20)
#狀態(tài)
buttonStop['state'] = 'disabled'
# 下一首
buttonNext = tkinter.Button(root,text='下一首',command =bottonNextClik)

buttonNext.place(x=260,y=10,width=50,height=20)

buttonNext['state'] = 'disabled'

#上一首
buttonPrev = tkinter.Button(root,text='上一首',command =bottonPrevClik)
buttonPrev.place(x = 330,y=10,width=50,height=20)
buttonPrev['state'] = 'disabled'

musicName = tkinter.StringVar(root,value='暫時(shí)沒有播放音樂')
labelName = tkinter.Label(root,textvariable=musicName)

labelName.place(x=10,y=30,width=260,height=20)

#添加控件
s = tkinter.Scale(root,label='音量',from_=0,to=1,orient=tkinter.HORIZONTAL,
     length=240,showvalue=0,tickinterval=2,resolution=0.1,command=control_voice)

s.place(x=50,y=50,width=200)

#啟動(dòng)消息循環(huán)
root.mainloop()

運(yùn)行效果:

在這里插入圖片描述

到此這篇關(guān)于python實(shí)現(xiàn)音樂播放和下載小程序功能的文章就介紹到這了,更多相關(guān)python--音樂播放和下載小程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python通過robert、sobel、Laplace算子實(shí)現(xiàn)圖像邊緣提取詳解

    python通過robert、sobel、Laplace算子實(shí)現(xiàn)圖像邊緣提取詳解

    這篇文章主要介紹了python通過robert、sobel、Laplace算子實(shí)現(xiàn)圖像邊緣提取詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式

    Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式

    這篇文章主要介紹了Python加載文件內(nèi)容的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • python實(shí)現(xiàn)單機(jī)五子棋對(duì)戰(zhàn)游戲

    python實(shí)現(xiàn)單機(jī)五子棋對(duì)戰(zhàn)游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)單機(jī)五子棋對(duì)戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • pytorch實(shí)現(xiàn)邏輯回歸

    pytorch實(shí)現(xiàn)邏輯回歸

    這篇文章主要為大家詳細(xì)介紹了pytorch實(shí)現(xiàn)邏輯回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 詳解Python查找誰刪了你的微信

    詳解Python查找誰刪了你的微信

    微信好友長時(shí)間不聯(lián)系就可能被對(duì)方刪除,但是微信也不會(huì)主動(dòng)通知你。那么我們就來用python寫一個(gè)工具查驗(yàn)一下誰刪除了你的微信
    2022-02-02
  • Python爬蟲抓取手機(jī)APP的傳輸數(shù)據(jù)

    Python爬蟲抓取手機(jī)APP的傳輸數(shù)據(jù)

    大多數(shù)APP里面返回的是json格式數(shù)據(jù),或者一堆加密過的數(shù)據(jù) 。這里以超級(jí)課程表APP為例,抓取超級(jí)課程表里用戶發(fā)的話題
    2016-01-01
  • python操作日期和時(shí)間的方法

    python操作日期和時(shí)間的方法

    經(jīng)常獲得了一個(gè)用戶提交的當(dāng)前日期,我們需要以這個(gè)日期為依據(jù)返回它的前一天、后一天的日期或者轉(zhuǎn)換操作等。用Python可以非常簡單的解決這些關(guān)于日期計(jì)算的問題
    2014-03-03
  • Python使用內(nèi)置json模塊解析json格式數(shù)據(jù)的方法

    Python使用內(nèi)置json模塊解析json格式數(shù)據(jù)的方法

    這篇文章主要介紹了Python使用內(nèi)置json模塊解析json格式數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Python使用內(nèi)置的json模塊實(shí)現(xiàn)json格式數(shù)據(jù)的解析、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • 使用python3 實(shí)現(xiàn)插入數(shù)據(jù)到mysql

    使用python3 實(shí)現(xiàn)插入數(shù)據(jù)到mysql

    今天小編就為大家分享一篇使用python3 實(shí)現(xiàn)插入數(shù)據(jù)到mysql,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python 模擬登陸的兩種實(shí)現(xiàn)方法

    Python 模擬登陸的兩種實(shí)現(xiàn)方法

    這篇文章主要介紹了Python 模擬登陸的兩種實(shí)現(xiàn)方法的相關(guān)資料,這里提供兩種方法一個(gè)是普通寫法寫的,另外一個(gè)是基于面向?qū)ο髮懙?,模擬登錄成功后才可能抓取內(nèi)容,需要的朋友可以參考下
    2017-08-08

最新評(píng)論

兴仁县| 手机| 祥云县| 株洲县| 阿克陶县| 昌都县| 沈丘县| 视频| 新郑市| 上思县| 黔南| 刚察县| 海林市| 婺源县| 钟山县| 浦江县| 沅陵县| 古浪县| 三穗县| 金阳县| 宜章县| 辉县市| 汕尾市| 钟祥市| 新昌县| 运城市| 平遥县| 武鸣县| 邢台市| 游戏| 壤塘县| 南召县| 梓潼县| 揭阳市| 同仁县| 扶余县| 泽普县| 呼和浩特市| 子洲县| 上高县| 中西区|