Downloading file using IE from python

后端 未结 8 912
无人及你
无人及你 2021-02-04 15:38

I\'m trying to download file with Python using IE:

from win32com.client import DispatchWithEvents

class EventHandler(object):
    def OnDownloadBegin(self):
            


        
8条回答
  •  攒了一身酷
    2021-02-04 16:16

    This works for me as long as the IE dialogs are in the foreground and the downloaded file does not already exist in the "Save As" directory:

    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");
    

提交回复
热议问题