Downloading file using IE from python

2024/10/16 1:23:33

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

from win32com.client import DispatchWithEventsclass EventHandler(object):def OnDownloadBegin(self):passie = DispatchWithEvents("InternetExplorer.Application", EventHandler)ie.Visible = 0ie.Navigate('http://website/file.xml')

After this, I'm getting a window asking the user where to save the file. How can I save this file automatically from python?

I need to use some browser, not urllib or mechanize, because before downloading file I need to interact with some ajax functionality.

Answer

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 Dispatchclass IeThread(threading.Thread):def run(self):pythoncom.CoInitialize()ie = Dispatch("InternetExplorer.Application")ie.Visible = 0ie.Navigate('http://website/file.xml')def PushButton(handle, label):if win32gui.GetWindowText(handle) == label:win32gui.SendMessage(handle, win32con.BM_CLICK, None, None)return TrueIeThread().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");
https://en.xdnf.cn/q/69219.html

Related Q&A

Good resources to start python for web development?

Im really interested in learning Python for web development. Can anyone point me in the right direction? Ive been looking at stuff on Google, but havent really found anything that shows proper documen…

django file upload: [Errno 13] Permission denied: /static

I am trying to upload several files in django. On my local maching where I use the djangos build in server everything works fine but on my productivity server I get this error:[Errno 13] Permission den…

efficient way to change the header of a file in Python

I am trying to write a python script to update the header (only the first line) of some huge files, but as the new header is not necessary to be the same size (in bytes) as the original one, is there a…

Converting a numpy array of dtype objects to dtype complex

I have a numpy array which I want to convert from an object to complex. If I take that array as dtype string and convert it, there is no problem:In[22]: bane Out[22]: array([1.000027337501943-7.3310852…

Python ZeroMQ PUSH/PULL -- Lost Messages?

I am trying to use python with zeroMQ in PUSH / PULL mode, sending messages of size 4[MB] every few seconds.For some reason, while it looks like all the messages are sent, ONLY SOME of them appear to h…

Using object as key in dictionary in Python - Hash function

I am trying to use an object as the key value to a dictionary in Python. I follow the recommendations from some other posts that we need to implement 2 functions: __hash__ and __eq__ And with that, I a…

Compressing request body with python-requests?

(This question is not about transparent decompression of gzip-encoded responses from a web server; I know that requests handles that automatically.)ProblemIm trying to POST a file to a RESTful web serv…

pyspark row number dataframe

I have a dataframe, with columns time,a,b,c,d,val. I would like to create a dataframe, with additional column, that will contain the row number of the row, within each group, where a,b,c,d is a group k…

Python mysql-connector hangs indefinitely when connecting to remote mysql via SSH

I am Testing out connection to mysql server with python. I need to ssh into the server and establish a mysql connection. The following code works: from sshtunnel import SSHTunnelForwarder import pymysq…

Smooth the edges of binary images (Face) using Python and Open CV

I am looking for a perfect way to smooth edges of binary images. The problem is the binary image appears to be a staircase like borders which is very unpleasing for my further masking process. I am att…