Python爬音乐--kugou

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')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值