【Python 爬虫 CASE】使用Selenium自动下载数据

一、需求

从目标网站登陆后,从指定页面的下载链接处点击下载,鼠标悬浮后指定格式的文件,该页面有多个分页,以id区分

二、实现

STEP1:构造浏览器
浏览器下载时每次都弹出弹窗询问,需要先禁用弹窗和设置下载路径

#火狐  开始-运行-firefox.exe -ProfileManager
#profile=webdriver.FirefoxProfile(r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\4a4m4hwv.ford')
#wd=webdriver.Firefox(firefox_profile=profile)

#谷歌
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'G:\\temp files'}#屏蔽弹窗,设置路径
options.add_experimental_option('prefs', prefs)
wd = webdriver.Chrome(options=options)

STEP2:登录网站

#打开网址
wd.get('网站登陆url')
wd.maximize_window()#窗口最大化,否则后面的一些元素找不到而报错
#输入用户名和密码,点击登录
user = wd.find_element_by_id('sawlogonuser')
user.send_keys('用户名')

user = wd.find_element_by_id('sawlogonpwd')
user.send_keys('mima123456')

wd.find_element_by_id('idlogon').click()

STEP3:下载文件
在这里插入图片描述

下载第1分页

wd.get('目标下载url')#跳转页面
wd.find_element_by_id('dashboard_page_1_tab').click()#选择table

#找到”导出“并点击
WebDriverWait(wd,10).until(EC.presence_of_element_located((By.NAME,'ReportLinkMenu')))
wd.find_element_by_name('ReportLinkMenu').click()

#找到”Excel"并鼠标悬浮
WebDriverWait(wd,10).until(EC.presence_of_element_located((By.LINK_TEXT,'Excel')))
a=wd.find_element_by_link_text('Excel')
ActionChains(wd).move_to_element(a).perform()#悬浮

#找到“Excel 2007+”并点击,执行下载操作
WebDriverWait(wd,10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT,'Excel 2007+')))
wd.find_element_by_partial_link_text('Excel 2007+').click()

第i分页的操作流程一模一样,id:‘dashboard_page_’+str(i)+’_tab’

三、整合


def main():
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time

    #火狐  开始-运行-firefox.exe -ProfileManager
    #profile=webdriver.FirefoxProfile(r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\4a4m4hwv.ford')
    #wd=webdriver.Firefox(firefox_profile=profile)

    #谷歌
    options = webdriver.ChromeOptions()
    prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'G:\\temp files'}#屏蔽弹窗,设置路径
    options.add_experimental_option('prefs', prefs)
    wd = webdriver.Chrome(options=options)

    #打开网址
    wd.get('网站登陆url')
    wd.maximize_window()#窗口最大化,否则后面的一些元素找不到而报错
    #输入用户名和密码,点击登录
    user = wd.find_element_by_id('sawlogonuser')
    user.send_keys('用户名')

    user = wd.find_element_by_id('sawlogonpwd')
    user.send_keys('密码123456')

    wd.find_element_by_id('idlogon').click()


    #下载文件
    def downloads(url,inlist):
        wd.get(url)#跳转页面
        for i in inlist:
            wd.find_element_by_id('dashboard_page_'+str(i)+'_tab').click()#选择table

            WebDriverWait(wd,10).until(EC.presence_of_element_located((By.NAME,'ReportLinkMenu')))
            wd.find_element_by_name('ReportLinkMenu').click()

            WebDriverWait(wd,10).until(EC.presence_of_element_located((By.LINK_TEXT,'Excel')))
            a=wd.find_element_by_link_text('Excel')
            ActionChains(wd).move_to_element(a).perform()#悬浮
            
            WebDriverWait(wd,10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT,'Excel 2007+')))
            wd.find_element_by_partial_link_text('Excel 2007+').click()

            
    url1='第一个下载页面url'
    list1=[0,1,2,3,4,7,9,10]
    downloads(url1,list1)
    url2='第二个下载页面url'
    list2=[0]
    downloads(url2,list2)
    time.sleep(5)
    wd.close()
    
if __name__=='__main__':main()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值