Search code examples
javascriptpythonasp.netweb-scrapingspynner

Use Python Spynner to download a file with a JavaScript doPostBack link


I am having trouble getting Python Spynner to download a file from an ASP webpage that has an anchor to href="javascript:__doPostBack('ProjectionBoard1$cmdCSV','')".

Example "Export Data"

Is there an easy way to do this? I have tried reverse engineering the JavaScript by analyzing the post request to no avail. If I execute the _doPostBack request in the console I get:

Resource interpreted as Document but transferred with MIME type text/csv: "http://www.fangraphs.com/leaders.aspx?pos=all&stats=pit&lg=all&qual=0&type=…son=2015&month=0&season1=2015&ind=0&team=18&rost=0&age=0&filter=&players=0"

Solution

  • Python Spynner will automatically download the file associated with an anchor to href tag (or JavaScript directed download) and save it to the working directory when clicked. There are two options for targeting and clicking the link to download:

    b = spynner.Browser()
    b.click("#someAnchorId", wait_load=True)
    

    or use JQuery to target the link

    b = spynner.Browser()
    b.runjs("jQuery('#someAnchorId').get(0).click()")
    b.wait(1)