from selenium import webdriver
from bs4 import BeautifulSoup
import urllib.request
from selenium.webdriver.common.action_chains import ActionChains
input_string = input('>>>输入您要下载的歌曲名:')
driver = webdriver.Firefox()
driver.get('http://www.kugou.com/')
a=driver.find_element_by_xpath('html/body/div[1]/div[1]/div[1]/div[1]/input')
a.send_keys(input_string)
driver.find_element_by_xpath('html/body/div[1]/div[1]/div[1]/div[1]/div/i').click()
for handle in driver.window_handles:
driver.switch_to_window(handle)
soup = BeautifulSoup(driver.page_source,'html.parser')
PageAll = len(soup.select('ul.list_content.clearfix > li'))
print(PageAll)
for i in range(1,PageAll+1):
j=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%d]/div[1]/a'%i).get_attribute('title')
print('%d.'%i + j)
choice=input("请输入你要下载的歌曲(输入序号):")
a=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%s]/div[1]/a'%choice)#定位
b=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%s]/div[1]/a'%choice).get_attribute('title')
actions=ActionChains(driver)
actions.move_to_element(a)
actions.click(a)
actions.perform()
for handle in driver.window_handles:
driver.switch_to_window(handle)
Local=driver.find_element_by_xpath('//*[@id="myAudio"]').get_attribute('src')
print("歌曲路径-------",Local)
def cbk(a, b, c):
per = 100.0 * a * b / c
if per > 100:
per = 100
print('%.2f%%' % per)
soup=BeautifulSoup(b)
name=soup.get_text()
path='C:\%s.mp3'%name
urllib.request.urlretrieve(Local, path, cbk)
print('%s.mp3---下载完成' % name + '\n\n')
Python爬音乐--kugou
最新推荐文章于 2024-05-01 20:07:07 发布