views:

191

answers:

2

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

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')

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.

+1  A: 

You don't need to use IE. You could use something like

import urllib2
data = urllib2.urlopen("http://website/file.xml").read()

Update: I see you've updated your question. If you need to use a browser, then clearly this answer isn't appropriate for you.

Further update: When you click the button which is generated by JavaScript, if the URL retrieved is not computed by the JavaScript, and only the button is, then you can perhaps retrieve that URL via urllib2. On the other hand, you might also need to pass a session cookie from your authenticated session.

Vinay Sajip
He/she said "I need to use some browser, not urllib or mechanize, because before downloading file I need to pass many ajax stuff."
Cristian Ciupitu
That wasn't in the original question.
Vinay Sajip
Before I will start downloading I need to login on website. Then click some links which will start some java scripts. Scripts are writing content of the website (without reload). This create new button on website which make possibility to download my file...So I don't think that I can use urlib2...
Adam
Unfortunately "button" is:<a [...] class="ptButton" onclick="exportSelected([...]); return false;">Export Selected</a>
Adam
A: 

If you can't control Internet Explorer using its COM interface, I suggest using the AutoIt COM to control its GUI from Python.

Cristian Ciupitu
Autolt look nice. But I want to write small application which just get this file and use data from it. I prefer some small smart solution...
Adam