import time import requests from selenium import webdriver import os from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36') driver = webdriver.Chrome(executable_path='chromedriver.exe', options=chrome_options) driver.maximize_window() driver.implicitly_wait(10) driver.get('http://tool.liumingye.cn/music/?page=searchPage') driver.find_element_by_css_selector('#input').send_keys('张杰') driver.find_element_by_css_selector('#search button:nth-child(2) i').click() """保存歌曲数据""" os.chdir(r"E:/ntmssFile/") def download(name, url): filename = '音乐\' + name + '.mp3' response = requests.get(url=url) with open(filename, mode='wb') as f: f.write(response.content) """解析网站数据获取歌曲名字以及歌曲下载地址""" while True: text = driver.find_element_by_css_selector('#player > div.aplayer-more').text if text == '下一页': try: time.sleep(3) driver.find_element_by_css_selector('#player > div.aplayer-more').click() except: continue elif text == '没有了': break lis = driver.find_elements_by_css_selector('#player li') f = 0 for li in lis: try: f += 1 time.sleep(3) name = li.find_element_by_css_selector('.aplayer-list-title').text li.find_element_by_css_selector('.aplayer-list-download').click() time.sleep(3) down_url = driver.find_element_by_css_selector('#m-download > div > div > div.modal-body > div:nth-child(6) > div.input-group-append > a.btn.btn-outline-secondary.download').get_attribute('href') time.sleep(3) driver.find_element_by_css_selector('#m-download > div > div > div.modal-header > button').click() download(name, down_url) print(name, down_url) except: continue
https://files.cnblogs.com/files/luweiweicode/chromedriver.zip 驱动下载