Ada*_*dam 7 python com internet-explorer
我正在尝试使用IE下载Python文件:
from win32com.client import DispatchWithEvents
class EventHandler(object):
def OnDownloadBegin(self):
pass
ie = DispatchWithEvents("InternetExplorer.Application", EventHandler)
ie.Visible = 0
ie.Navigate('http://website/file.xml')
Run Code Online (Sandbox Code Playgroud)
在此之后,我得到一个窗口,询问用户保存文件的位置.如何从python自动保存此文件?
我需要使用一些浏览器,而不是urllib或mechanize,因为在下载文件之前我需要与一些ajax功能进行交互.
只要IE对话框在前台并且"另存为"目录中不存在下载的文件,这对我有用:
import time
import threading
import win32ui, win32gui, win32com, pythoncom, win32con
from win32com.client import Dispatch
class IeThread(threading.Thread):
def run(self):
pythoncom.CoInitialize()
ie = Dispatch("InternetExplorer.Application")
ie.Visible = 0
ie.Navigate('http://website/file.xml')
def PushButton(handle, label):
if win32gui.GetWindowText(handle) == label:
win32gui.SendMessage(handle, win32con.BM_CLICK, None, None)
return True
IeThread().start()
time.sleep(3) # wait until IE is started
wnd = win32ui.GetForegroundWindow()
if wnd.GetWindowText() == "File Download - Security Warning":
win32gui.EnumChildWindows(wnd.GetSafeHwnd(), PushButton, "&Save");
time.sleep(1)
wnd = win32ui.GetForegroundWindow()
if wnd.GetWindowText() == "Save As":
win32gui.EnumChildWindows(wnd.GetSafeHwnd(), PushButton, "&Save");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8625 次 |
| 最近记录: |