python自动下载文件_Python selenium 文件自动下载 (自动下载器)

#!/usr/bin/env python3#-*- coding:utf-8 -*-

from selenium importwebdriverfrom getUserInfo importXlUserInfoimportthreadingclassAutoDownload(object):def __init__(self,file_type,args, args2):

self.file_type=file_type

self.args=args

self.args2=args2defopenBrower(self):

self.profile=webdriver.FirefoxProfile()

self.profile.accept_untrusted_certs=Trueif self.args2['downloadpath'] isNone:

self.profile.set_preference('browser.download.dir', 'c:\\')else:

self.profile.set_preference('browser.download.dir', self.args2['downloadpath'])print(self.args2['downloadpath'])

self.profile.set_preference('browser.download.folderList', 2)

self.profile.set_preference('browser.download.manager.showWhenStarting', False)if self.file_type == 'xml':

self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml')elif self.file_type == 'uxz':

self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml')elif self.file_type == 'txt':

self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')else:

self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')#3,6 xml,tml file

#profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml')

#2,4 txt,chg file

#profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')

self.driver = webdriver.Firefox(firefox_profile=self.profile)

self.driver.implicitly_wait(30)returnself.driverdefopenUrl(self):try:

self.driver.get(self.args2['url'])

self.driver.maximize_window()except:print("Failed to get {}".format(self.args2['url']))returnself.driverdeflogin(self):'''user_name

pwd_name

logIn_name'''self.driver.find_element_by_name(self.args['user_name']).send_keys(self.args2['uname'])if isinstance(self.args2['pwd'],float):

self.driver.find_element_by_name(self.args['pwd_name']).send_keys(int(self.args2['pwd']))else:

self.driver.find_element_by_name(self.args['pwd_name']).send_keys(self.args2['pwd'])

self.driver.find_element_by_name(self.args['logIn_name']).click()

self.driver.implicitly_wait(10)returnself.driverdefdownload(self):

self.driver.implicitly_wait(15)

self.driver.find_element_by_link_text(self.args['Search_Forms_text']).click()

self.driver.implicitly_wait(30)

self.driver.find_element_by_id(self.args['OSS_Num_type_id']).send_keys(int(self.args2['OSS_num']))

self.driver.find_element_by_id(self.args['Search_button_id']).click()

self.driver.implicitly_wait(10)

self.driver.find_element_by_link_text(str(int(self.args2['OSS_num']))).click()

self.driver.implicitly_wait(20)#Attachments_text

self.driver.find_element_by_link_text(self.args['Attachments_text']).click()

self.driver.implicitly_wait(10)if self.file_type == 'xml':

self.driver.find_element_by_xpath('//table[4]//tr[3]/td[1]/a').click()

self.driver.implicitly_wait(30)

self.driver.find_element_by_xpath('//table[4]//tr[6]/td[1]/a').click()elif self.file_type == 'uxz':

self.driver.find_element_by_xpath('//table[4]//tr[5]/td[1]/a').click()elif self.file_type == 'txt':

self.driver.find_element_by_xpath('//table[4]//tr[2]/td[1]/a').click()#driver.find_element_by_xpath('//table[4]//tr[6]/td[1]/a').click()

self.driver.implicitly_wait(30)

self.driver.find_element_by_xpath('//table[4]//tr[4]/td[1]/a').click()else:

self.driver.quit()defquit(self):

self.driver.quit()defRun(self):

self.openBrower()

self.openUrl()

self.login()

self.download()

self.quit()if __name__ == '__main__':

xl= XlUserInfo('userInfo.xlsx')

userinfo= xl.get_sheetinfo_by_name('userInfo')

webinfo= xl.get_sheetinfo_by_name('WebEle')print(userinfo)print(webinfo)

down_txt= AutoDownload('txt',webinfo,userinfo)

down_xml= AutoDownload('xml',webinfo,userinfo)

threads=[]

t1= threading.Thread(target=down_txt.Run)

t2= threading.Thread(target=down_xml.Run)

threads.append(t1)

threads.append(t2)for t inthreads:

t.start()for i inthreads:

i.join()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值