zoukankan      html  css  js  c++  java
  • webdriver 抓取歌曲

    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  驱动下载

    故乡明
  • 相关阅读:
    python 协程
    python 进程池的使用
    python 多进程数据交互及共享
    python 多进程
    技术博客与技术日记
    理解闭包
    jWriter一个基于jQuery的阅读写作网站的效果库
    ubuntu下phpmyadmin配置问题解决
    避免明文保存用户密码
    如何用css实现类似简书的纵向导航/竖排导航
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14355430.html
Copyright © 2011-2022 走看看