1. 程式人生 > >python+selenium 下載網易雲音樂 支持批量下載

python+selenium 下載網易雲音樂 支持批量下載

exc [] attr requests ons disable inf src chrom

import os
import re
import requests
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException



class Wy_music():
def __init__(self, name):
self.option=webdriver.ChromeOptions()
self.option.add_argument(‘disable-infobars‘)

self.driver = webdriver.Chrome(options=self.option)
self.i_list = []
self.id_list = []
self.name_list = []
self.music_name = name


def get_id_name(self):
aim_url=‘https://music.163.com/#/search/m/?s=‘+self.music_name+‘&type=1‘ #目標歌曲的url
self.driver.get(aim_url)
self.driver.switch_to.frame(‘contentFrame‘) #切換到frame中,不然會定位失敗

div_list =self.driver.find_elements_by_xpath(‘.//div[@class="srchsongst"]/div‘)
print(len(div_list))


for div in div_list:
#try 語句先是嘗試查詢一邊name url singer 如果不存在再嘗試except中的內容
#因為不同的出現兩種不同的xpath,應該是後來修改過html
try:
name = div.find_element_by_xpath(‘.//div[@class="text"]//b‘).text

url = div.find_element_by_xpath(‘.//div[@class="td w0"]//a‘).get_attribute(‘href‘)
singer = div.find_element_by_xpath(‘.//div[@class="td w1"]//a‘).text
except NoSuchElementException:
name = div.find_element_by_xpath(‘.//div[@class="text"]//b‘).text

url = div.find_element_by_xpath(‘.//div[@class="td w0"]//a‘).get_attribute(‘href‘)
singer = div.find_element_by_xpath(‘.//div[@class="td w1"]/div‘).text

id = re.search(r‘id=(\d+)‘, url).group(1)
i = div_list.index(div)
self.i_list.append(i)
self.id_list.append(id)
self.name_list.append(name+"_"+singer)
print(i,name,singer)




name_list = list(zip(self.id_list, self.name_list))
print(‘id_name‘,name_list)
song_dict = dict(zip(self.i_list, name_list))
print(‘最終id_歌曲‘,song_dict)
return song_dict


def download_music(self, url, song_name):
print(‘{}正在下載‘.format((song_name)))
headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0‘}
response = requests.get(url,headers=headers)
full_songname = song_name + ‘.mp3‘
with open(‘{}‘.format(full_songname), ‘wb‘) as f:
f.write(response.content)
print(‘{}下載完成‘.format(song_name))


def choose_musicid(self, song_dict):
num_str = input(‘請輸入你需要下載歌曲的編號,以空格隔開: ‘)
num_list = num_str.split(‘ ‘)
for num in num_list:
try:
num = int(num)
except Exception as e:
print(e, ‘請輸入整數‘)
if num > len(song_dict):
print(‘請輸入有效數字‘)
url =‘https://music.163.com/song/media/outer/url?id={}‘.format(song_dict[num][0])
print(url)
song_name = song_dict[num][1]
# print(‘歌曲名——歌手名‘,song_name)
yield url,song_name



if __name__ == ‘__main__‘:
name = input(‘請輸入你要搜索的歌名或歌手:‘)
wy = Wy_music(name)
song_dict = wy.get_id_name()
for url, song_name in wy.choose_musicid(song_dict):
wy.download_music(url, song_name)

目前未解決的小問題,再創建文件時,有多個歌手時,會有 “/” 隔開,這會導致創建文件失敗

python+selenium 下載網易雲音樂 支持批量下載