Search code examples
pythoncsvpython-2.7urllib2

Trigger a websites file download with python


There is a stock screening website called finviz. You can set up specific parameters for it to screen, and then there is a button in the bottom right corner that lets you export the results as a .cvs file.

I would like to create a script, in python 2.7, that will download and analyze the file. I imagine I can use urllib2 to access the website, but how can I trigger the export, and then read from that resulting file? Using the standard urllib2.urlopen(url).read(), returns an html file for the entire site, and not the export I need.


Solution

  • So it turns out, at least in this case, the export button is really a link to a different url. So where the screener's url might be: http://finviz.com/screener.ashx?v=111&f=sh_price_u1. The export version of the url is: http://finviz.com/export.ashx?v=111&f=sh_price_u1. The second url has the funcitonality of triggering download, so instead of urllib2.urlopen("http://finviz.com/screener.ashx?v=111&f=sh_price_u1").read() I need urllib2.urlopen("http://finviz.com/export.ashx?v=111&f=sh_price_u1").read()