学习之余,我要带大家做一回彻彻底底的白嫖党!!!
我是用FireFox浏览器实现的,代码仅仅供参考!
本次python实战教学的内容是亲手制作一个网易云免费下载器(版权意识,VIP不能下载~—~)
代码如下:
from tkinter import * from urllib.request import urlretrieve import requests import os from selenium import webdriver def song_load(item): song_id = item['song_id'] song_name = item['song_name'] song_url = 'https://music.163.com/song/media/outer/url?id={}.mp3'.format(song_id) os.makedirs('E:Python_Music_DownLoad', exist_ok=True) path = 'E:Python_Music_DownLoad{}.mp3'.format(song_name) text_list.insert(END, '歌曲:{} 正在下载……'.format(song_name)) text_list.see(END) text_list.update() urlretrieve(song_url, path) text_list.insert(END, '歌曲:下载完毕!'.format(song_name)) text_list.see(END) text_list.update() def get_music_name(): name = entry.get() url = 'https://music.163.com/#/search/m/?s={}&type=1'.format(name) # 搜索歌曲页面 driver = webdriver.Firefox() driver.get(url) driver.switch_to.frame('g_iframe') resp = driver.find_element_by_id('m-search') a_id = resp.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div/div/div[1]/div[2]/div/div/a').get_attribute("href") song_id = a_id.split('=')[-1] #print(song_id) song_name = resp.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div/div/div[1]/div[2]/div/div/a/b').get_attribute("title") #print(song_name) item = {} item['song_id'] = song_id item['song_name'] = song_name song_load(item) driver.quit() #get_music_name() root = Tk() root.title('安安音乐下载器(仅支持网易云)') root.geometry('600x430+500+200') label = Label(root, text='What do you want to listen?', font=('Ink Free', 20)) label.grid(row=0, column=0) entry = Entry(root, font=('方正舒体', 16)) entry.grid(row=0, column=1) text_list = Listbox(root, font=('华文新魏', 14), width=50, heigh=15) text_list.grid(row=1, columnspan=2) buttonW = Button(root, text='Download', font=('Algerian', 18), command=get_music_name) buttonW.grid(row=2, column=0, sticky=W) buttonE = Button(root, text='Exit', font=('Algerian', 18), command=root.destroy) buttonE.grid(row=2, column=1, sticky=E) root.mainloop()