码迷,mamicode.com
首页 > 其他好文 > 详细

某云音乐下载器

时间:2020-06-08 09:26:38      阅读:683      评论:0      收藏:0      [点我收藏+]

标签:try   窗口   dir   列表框   art   get   dex   创建文件夹   type   

from urllib.request import urlretrieve
from tkinter import *
from selenium import webdriver
import os


#功能
#https://music.163.com/#/search/m/?s=%E4%B8%80%E7%9B%B4%E5%BE%88%E5%AE%89%E9%9D%99&type=1
#http://music.163.com/song/media/outer/url?id=5260494.mp3

#下载歌曲
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)
    print(song_url)
    #创建文件夹
    os.makedirs(‘music‘,exist_ok=True)
    path = ‘music\{}.mp3‘.format(song_name)
    text.insert(END,‘歌曲:{},正在下载...‘.format(song_name))
    #文本框滑动
    text.see(END)
    #更新
    text.update()
    #下载
    urlretrieve(song_url, path)
    text.insert(END, ‘{}下载完毕,请试听.‘.format(song_name))
    # 文本框滑动
    text.see(END)
    # 更新
    text.update()




#歌曲id
def get_music_name():
    name = entry.get()
    url = ‘https://music.163.com/#/search/m/?s={}&type=1‘.format(name)
    print(url)
    #隐藏浏览器
    option = webdriver.ChromeOptions()
    option.add_argument(‘--headless‘)
    driver =webdriver.Chrome(chrome_options=option)
    driver.get(url=url)
    driver.switch_to.frame(‘g_iframe‘) #id name index
    #歌曲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‘)
    print(a_id)
    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)

    #构建字典 id name
    item = {}
    item[‘song_id‘] = song_id
    item[‘song_name‘] = song_name

    driver.quit()
    #下载歌曲
    song_load(item)

#界面

#1.创建画布
root = Tk()
#2.添加标题
root.title(‘网易云音乐下载器‘)
#3.设置窗口大小
root.geometry(‘560x450+600+300‘)
#4.标签控件
label = Label(root,text=‘请输入歌曲名称:‘,font = (‘华文行楷‘,20))
#5.定位
label.grid()

#6.输入框
entry = Entry(root,font = (‘隶书‘,20))
#7.定位
entry.grid(row = 0,column = 1)
#8.列表框
text = Listbox(root,font=(‘隶书‘,16),width=50,height=15)
#定位
text.grid(row=1,columnspan=2)
#下载按钮
button = Button(root,text=‘开始下载‘,font=(‘隶书‘,15),command=get_music_name)
button.grid(row=2,column=0)
#退出程序
button1 = Button(root,text=‘退出程序‘,font=(‘隶书‘,15),command=root.quit)
button1.grid(row=2,column=1)

#显示界面
root.mainloop()

技术图片

 

参考:

零基础学编程,Python爬虫与GUI制作音乐下载工具!

https://www.bilibili.com/video/BV1pz411i7yq

 

python3的urlretrieve()方法的作用与使用

https://blog.csdn.net/u012424313/article/details/82222188

某云音乐下载器

标签:try   窗口   dir   列表框   art   get   dex   创建文件夹   type   

原文地址:https://www.cnblogs.com/modiqiang/p/13063365.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!