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)
目前未解决的小问题,再创建文件时,有多个歌手时,会有 “/” 隔开,这会导致创建文件失败