selenium 免费下载qq vip音乐

时间:2024-04-17 17:30:55

1
from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from selenium.webdriver.support import expected_conditions as EC 4 from selenium.webdriver.support.wait import WebDriverWait 5 import time 6 from selenium.webdriver.common.action_chains import ActionChains 7 from urllib import request 8 driver = webdriver.Firefox() 9 songname = input("输入你的歌曲名字:") 10 def get_url(): 11 qq_url = \'https://y.qq.com/\' 12 driver.get(qq_url) 13 driver.implicitly_wait(10) 14 # 等待10 sec时间加载,加载、确定弹出框 15 WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CLASS_NAME,\'popup__close\'))) 16 # 关闭弹出框 17 driver.find_element_by_class_name(\'popup__icon_close\').click() 18 time.sleep(5) 19 #鼠标悬停到搜索框 20 above = driver.find_element_by_class_name(\'search_input__input\') 21 # 鼠标移动到搜索框 22 ActionChains(driver).move_to_element(above).perform() 23 # 在搜索框里输入“说好不哭” ,search_input__input 搜索框的class 24 driver.find_element_by_class_name(\'search_input__input\').send_keys(songname) 25 # 点击搜索按钮 26 driver.find_element_by_xpath(\'//button[@class="search_input__btn"]\').click() 27 # 找到 28 song_url = driver.find_element_by_class_name(\'songlist__songname_txt\').find_element_by_tag_name(\'a\').get_attribute(\'href\') 29 print(\'下载的歌曲URL地址为:\' ,song_url) 30 return song_url 31 def down_song(song_url): 32 js = \'window.open("http://www.douqq.com/qqmusic/");\' 33 driver.execute_script(js) 34 # QQ_handle = driver.current_window_handle 35 # print("当前操作的窗口为:", QQ_handle) 36 handles = driver.window_handles 37 # print(\'当前浏览器的所有窗口,\', handles) 38 driver.switch_to.window(handles[-1]) 39 driver.find_element_by_id(\'mid\').send_keys(song_url) 40 time.sleep(10) 41 driver.find_element_by_id(\'sub\').click() 42 time.sleep(10) 43 mp3_url = driver.find_element_by_id(\'m4a\').text 44 if mp3_url: 45 print(\'正在下载-{}-,请耐心等待。。。\'.format(songname)) 46 request.urlretrieve(mp3_url,\'{}.mp3\'.format(songname)) 47 print(\'下载完成,THKS\') 48 else: 49 print(\'没有获取到,需要重新获取\') 50 driver.quit() 51 52 if __name__ == \'__main__\': 53 song_url = get_url() 54 down_song(song_url)