python Selenium實現(xiàn)付費音樂批量下載的實現(xiàn)方法
必備環(huán)境
廢話
每年回家都要幫我爸下些音樂,這對我來說都是輕車熟路!可當我打開網(wǎng)易云點擊下載按鈕的時候,可惜已物是人非啦!
開個 VIP 其實也不貴,臨時用用也就¥15!但 IT 男的尊嚴必須要有,于是開始徜徉于搜索引擎中
最后在知乎中,搜索到一個網(wǎng)址VIP付費音樂解析
P.S.
再次感謝提供該服務的作者!如果你下載的音樂數(shù)量不多,直接這里搜索下載,下載后修改文件名即可!并且在這個網(wǎng)址中點擊播放列表
-點擊同步
,可以同步網(wǎng)易云的歌單!之后批量下載即是下載這些網(wǎng)易云的歌單!但是下載某個歌單中的幾百首歌,手動下載就不現(xiàn)實了!在點擊同步
中需要輸入你的網(wǎng)易云 UID,這 UID 的獲取方式如下:第一步打開網(wǎng)易云隨便選中一首歌,右鍵復制鏈接
然后隨便找個地方粘貼這個鏈接,例如https://music.163.com/song?id=25727803&userid=275613591
最后這串數(shù)字就是 UID!
程序運行環(huán)境
第一步安裝一個python3
,這個簡單吧!貼上我的版本 python3.65,安裝時注意勾選Add in path
第二步下載FFmpeg
,這是用來解析視頻和音頻的,作為you-get
的輔助工具,下載點這里,下載后解壓添加環(huán)境變量即可
第三步安裝you-get
,這是個下載視頻音頻的神器,有興趣可以深入研究!之后我打算寫個下載任意視頻的工具,嘿嘿這是后話了!安裝方式很簡單pip install you-get
環(huán)境配置就這樣,還是非常輕松的,下面會解釋下代碼
源碼
完整代碼
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time, os # import threding def get_music_name_link(): main_handle = browser.current_window_handle fp = open('E:\\Project_PY\\file\\musiclink.txt','wb') fp2 = open('E:\\Project_PY\\file\\musicname.txt','wb') try: for i in list(range(2,400)): browser.switch_to_window(main_handle) txt = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[%d]' % i).text + '\n' fp2.write(bytes(txt,encoding='utf-8')) location = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[%d]' % i) ActionChains(browser).move_to_element(location).perform() browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[%d]/span[5]/div/span[2]' % i).click() time.sleep(2) all_handles = browser.window_handles browser.switch_to_window(all_handles[-1]) # lastest url_link = browser.current_url + '\n' fp.write(bytes(url_link,encoding='utf-8')) browser.close() except Exception as e: print('get_music_name_link meet some problem! {}'.format(e)) fp.close() fp2.close() def download_music(list_name): with open('E:\\Project_PY\\file\\musicname.txt','r',encoding='utf-8') as fp1: music_name = fp1.readlines() len1 = len(music_name) fp2 = open('E:\\Project_PY\\file\\musicname_format.txt','w',encoding='utf-8') for i in range(3,len1,4): music_name_format = music_name[i].strip() + '\n' fp2.write(music_name_format) fp2.close() with open('E:\\Project_PY\\file\\musiclink.txt','r',encoding='utf-8') as fp1: with open('E:\\Project_PY\\file\\musicname_format.txt','r',encoding='utf-8') as fp2: for music_link,music_name in zip(fp1.readlines(),fp2.readlines()): you_get_link = 'you-get "{}" -o "E:\\Project_PY\\file\\music\\{}" -O "{}"'.format(music_link.strip(),list_name,music_name.strip()) you_get_link = you_get_link.strip() # print(you_get_link) os.system(you_get_link) url = 'http://music.zhuolin.wang/' uid = input('please input your uid:') options = webdriver.FirefoxOptions() options.add_argument('--headless') browser = webdriver.Firefox(firefox_options=options) browser.implicitly_wait(8) browser.get(url) # browser.maximize_window() browser.set_window_size(1000,100000) browser.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/span[3]').click() # scroll = browser.find_element_by_xpath('//*[@id="mCSB_1_dragger_vertical"]') # ActionChains(browser).drag_and_drop_by_offset(scroll,0,100).perform() # time.sleep(2) all_handles = browser.window_handles browser.switch_to_window(all_handles[-1]) # lastest time.sleep(1) browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[1]/div/span/div[2]/span').click() all_handles = browser.window_handles browser.switch_to_window(all_handles[-1]) # lastest time.sleep(1) browser.find_element_by_xpath('/html/body/div[6]/div[2]/input').send_keys(uid) browser.find_element_by_xpath('/html/body/div[6]/div[3]/a[1]').click() # t1 = threading.Thread(target=get_music_name) # t2 = threading.Thread(target=get_music_link) # t3 = threading.Thread(target=download_music) for i in list(range(3,100)): try: print('downloading song_list{}! please waiting....'.format(i)) browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[1]/div/div[%d]/img' % i).click() dir_name = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[1]/div/div[%d]/p' % i).text time.sleep(1) get_music_name_link() download_music(dir_name) browser.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/span[3]').click() time.sleep(1) all_handles = browser.window_handles browser.switch_to_window(all_handles[-1]) # lastest time.sleep(5) except Exception as e: print('get_song_list meet some problem! {}'.format(e)) browser.quit()
核心代碼解釋
總共有三個函數(shù):
- 函數(shù)一
get_music_name_link
,主要是獲取音樂名稱以及音樂的下載鏈接 - 函數(shù)二
download_music
,獲取歌單名稱,然后拼接下載鏈接和音樂名,調(diào)用you-get
開始下載到對應目錄 - 函數(shù)三
main
,主要是利用 UID 獲取歌單,以及批量下載歌單中的曲目
需要注意的幾個點:
1.使用了sleep(1)
休眠一秒,如果網(wǎng)絡較慢需要將所有的sleep
休眠時間加長
2.所有的路徑需要自己根據(jù)本機修改
3.如果要修改代碼一定要注意switch_to_window
來切換窗口
4.73行的for i in list(range(3,100))
是用來選擇下載的歌單,歌單從 1 開始計數(shù)
程序演示
輸入網(wǎng)易云的 UID!
然后靜靜的等待即可...此過程中會有部分音樂的播放聲音,不喜歡可以開靜音下
當所有的鏈接解析完成后就會調(diào)用you-get
下載,此過程會自動創(chuàng)建與歌單名相同的文件夾
下載完成后
P.S.
如果要下載所有歌單,就不需要修改代碼,直接輸入網(wǎng)易云的 UID 運行即可!如果要下載某個具體的歌單只需要改動73行的這個循環(huán)for i in list(range(3,100))
,所以說程序還是比較簡單的,缺點可能就是沒時間寫 UI,而且也不太會 pyqt 之類的,只會點 MFC!所以將就用吧,功能還是很齊全的!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pandas pivot_table() 按日期分多列數(shù)據(jù)的方法
今天小編就為大家分享一篇pandas pivot_table() 按日期分多列數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Anaconda安裝時默認python版本改成其他版本的兩種方式
這篇文章主要給大家介紹了關(guān)于Anaconda安裝時默認python版本改成其他版本的兩種方式,anaconda是一個非常好用的python發(fā)行版本,其中包含了大部分常用的庫,需要的朋友可以參考下2023-10-10Pycharm無法使用已經(jīng)安裝Selenium的解決方法
今天小編就為大家分享一篇Pycharm無法使用已經(jīng)安裝Selenium的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python循環(huán)接收http請求數(shù)據(jù)方式
這篇文章主要介紹了python循環(huán)接收http請求數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06