python爬虫 selenium+Autolt 实现IE网页中的文件下载

Python使用selenium时,chrome和foxfire都可以通过设置来下载文件,但是IE没有相应的设置。在IE中当点击下载按钮时,会弹出一个下载窗口,该窗口不能用selenium来控制。目前可以利用Autolt来控制该窗口。

除了该方法,若可以知道下载网址,则只需直接 from urllib.request import urlopen打开网页下载即可。否则,还是需要Autolt来下载。下面为该种方法下载的过程。


#1、在Scite Script Edit中写入如下代码,并编译成exe文件
Local $pathToSave=$CmdLine[1]
; get the handle of main window
Local $windHandle= WinGetHandle("[Class:IEFrame]", "")
Local $winTitle = "[HANDLE:" & $windHandle & "]"
; Select save as option
WinActivate ($winTitle, "")
Send("{F6}")
sleep(500)
Send("{TAB}")
sleep(500)
Send("{DOWN}")
sleep(500)
Send("A")

; Save as dialog
; wait for Save As window
WinWait("另存为")
; activate Save As window
WinActivate("另存为")
ControlFocus("另存为","","ToolbarWindow324")
ControlClick("另存为","","ToolbarWindow324","left",1,300,10)
Sleep(2000)
Send($pathToSave)
Sleep(2000)
Send("{ENTER}")
Sleep(2000)
ControlClick("另存为","","Button2")

2、在python中调用上面的exe文件
;===Execution of above code through selenium in python=====
import subprocess
import log
file_path='D:\work2017\file'
cmd='D:/work2017/Autolt/test.exe'+' '+file_path
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True,
                         stderr=subprocess.PIPE)
out, err = p.communicate()

if p.returncode == 0:

    print('ok')

你可能感兴趣的:(python爬虫 selenium+Autolt 实现IE网页中的文件下载)