Update 大作业

大作业
Qv5o9fryk 5 years ago
parent 2f9dc102ae
commit e207bfe13a

@ -8,40 +8,37 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
#下载歌曲
# 下载歌曲
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)
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)
text.insert(END, '歌曲:{},正在下载...'.format(song_name))
text.insert(END, '歌曲:{},正在下载...请稍后...'.format(song_name))
with open("music/"+song_name + ".mp3", mode="wb") as f:
f.write(requests.get(song_url).content)
#文本框滚动
# 文本框滚动
text.see(END)
#更新
# 更新
text.update()
#下载
#urlretrieve(song_url, path)
# 创建歌曲文件夹
os.makedirs('music', exist_ok=True)
# 下载
with open("music/" + song_name + ".mp3", mode="wb") as f:
f.write(requests.get(song_url).content)
# urlretrieve(song_url, path)
text.insert(END, '{} 下载完毕'.format(song_name))
text.insert(END, '{} 下载完毕,欢迎试听'.format(song_name))
# 文本框滚动
text.see(END)
# 更新
text.update()
#歌曲id
# 歌曲id
def get_music_name():
name = entry.get()
@ -53,13 +50,13 @@ def get_music_name():
driver = webdriver.Chrome(options=option)
driver.get(url=url)
driver.switch_to.frame('g_iframe')
#获取歌曲id
# 获取歌曲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)
#获取歌曲name
# 获取歌曲name
song_name = req.find_element(By.XPATH, './/div[@class="item f-cb h-flag "]/div[2]//b').get_attribute("title")
# print(song_name)
@ -73,38 +70,44 @@ def get_music_name():
song_load(item)
# 界面生成及其功能
# 创建一个窗口
root = Tk()
# 窗口标题
root.title("网易音乐下载工具")
image=PhotoImage(file='E:/python/01.png')
root.geometry('540x390+550+230')
root=Tk()
root.title("便捷音乐下载工具")
backlable=Label(root,width=50,height=30,image=image)
root.geometry('550x390+550+230')
# 设置标签
label = Label(root, text="请输入下载的歌曲:", font=('华文行楷', 15))
label = Label(root, text="请输入下载的歌曲:", font='楷体 19 bold', fg='blue')
label.grid(row=0, column=0) # 网格式布局
entry = Entry(root, font=('微软雅黑', 20))
# 设置输入框
entry = Entry(root, font='楷体 15 bold', fg='red')
entry.grid(row=0, column=1)
text = Listbox(root, font=('微软雅黑', 15), width=45, height=10)
text.grid(row=1, columnspan=2)
# 设置显示文本
text = Listbox(root, font=('微软雅黑', 15), fg='red', width=45, height=10)
button1 = Button(root, text="开始下载", font=('微软雅黑', 15), command=get_music_name)
text.grid(row=1, columnspan=2)
# 设置“开始下载按钮”及其功能
button1 = Button(root, text="开始下载", font='微软雅黑 20 bold', fg='green', command=get_music_name)
button1.grid(row=2, column=0, sticky=W)
# sticky对齐方式 N S W E
button2 = Button(root, text="退出", font=('微软雅黑', 15), command=root.quit)
# 设置“退出”按钮及其功能
button2 = Button(root, text="退出", font='微软雅黑 20 bold', fg='purple', command=root.quit)
button2.grid(row=2, column=1, sticky=E)
# 窗口循环
root.mainloop()

Loading…
Cancel
Save