Python:从网页下载文件,ashx

时间:2016-10-31 08:26:36

标签: python download urllib2 ashx

this网页上有一个"导出到Excel"按钮。 与此链接关联的命令应为:

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

如何从Python脚本调用此命令来下载文件? 我试过的是:

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

1 个答案:

答案 0 :(得分:1)

您可以使用请求模块:

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/