本帖最后由 苹果刺客 于 2021-2-18 18:50 编辑
[Python] 纯文本查看 复制代码 from tkinter import *
from selenium import webdriver
import os
from urllib.request import urlretrieve
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)
os.makedirs('down', exist_ok=True)
path = 'down\{}.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()
def get_music_name():
name = entry.get()
url = 'https://music.163.com/#/search/m/?s={}&type=1'.format(name)
option = webdriver.ChromeOptions()
option.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=option)
# driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame('g_iframe')
# 此处获取歌曲id序列
song_id_1 = driver.find_element_by_class_name('text')
s = song_id_1.find_element_by_tag_name('a')
song_id_2 = s.get_attribute('href')
song_id = song_id_2.split('=', 2)[1]
# print(song_id)
# 此处获取歌曲名称
song_name_1 = song_id_1.find_element_by_tag_name('a')
song_name = song_name_1.find_element_by_tag_name('b').get_attribute('title')
# print(song_name)
driver.quit()
item = {'song_id': song_id, 'song_name': song_name}
song_load(item)
# get_music_name()
root = Tk()
root.title('网易云音乐下载')
root.geometry("400x380+200+180")
label = Label(root, text='大哥~想听啥呢:', font=20)
label.grid()
entry = Entry(root, font=20)
entry.grid(row=0, column=1)
text = Listbox(root, font=20, width=50, height=18)
text.grid(row=1, columnspan=2)
button = Button(root, text='下载', font=20, command=get_music_name)
button.grid(row=2, column=0, sticky=W)
button1 = Button(root, text='退出', font=20, command=root.quit)
button1.grid(row=2, column=1, sticky=E)
root.mainloop()
|