Search code examples
pythondownloadurllib2ashx

Python: download file from webpage, ashx


On this webpage there is an "Export to Excel" button. The command associated with this link should be:

https://www.animasgr.it/IT/Prodotti/Quotazioni-e-Performance/_layouts/15/GetExcel.ashx?type=QuoteFondo&code=A60A&i=03.06.1985&f=27.10.2016

How can I call this command from a Python script to dowload the file? What I tried is:

response = urllib2.urlopen(https://www.animasgr.it/IT/Prodotti/Quotazioni-e-Performance/_layouts/15/GetExcel.ashx?type=QuoteFondo&code=A60A&i=03.06.1985&f=27.10.2016)

In [12]: response
Out[12]: <addinfourl at 4504653336 whose fp = <socket._fileobject object at 0x10cf9e550>>

Solution

  • You can use requests module:

    import requests
    
    url_file = 'https://www.animasgr.it/IT/Prodotti/Quotazioni-e-Performance/_layouts/15/GetExcel.ashx?type=QuoteFondo&code=A60A&i=03.06.1985&f=27.10.2016'
    resp = requests.get(url_file)
    
    with open('anyfilename.xls', 'wb') as f:
        f.write(resp.content)
    

    http://docs.python-requests.org/en/latest/