-3

I've never worked with web pages before and I'd like to know how best to automate the following through programming/scripting:

  1. go to http://financials.morningstar.com/ratios/r.html?t=GMCR&region=USA&culture=en_US
  2. invoke the 'Export to CSV' button near the top right
  3. save this file into local directory
  4. parse file

Part 4 doesn't need to use the same language as for 1-3 but ideally I would like to do everything in one shot using one language.

I noticed that if I hover my mouse over the button it says: javascript:exportKeyStat2CSV(); Is this a java function I could call somehow?

Any suggestions are appreciated.

sentence
  • 8,213
  • 4
  • 31
  • 40
Colin
  • 415
  • 3
  • 14

2 Answers2

1

It's a JavaScript function, which is not Java!

At first glance, this may seem like you need to execute Javascript to get it done, but if you look at the source of the document, you can see the function is simply implemented like this:

    function exportKeyStat2CSV(){
        var orderby = SRT_keyStuts.getOrderFromCookie("order");
        var urlstr = "//financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNAS:GMCR&region=usa&culture=en-US&cur=&order="+orderby;
        document.location = urlstr;
    }

So, it builds a url, which is completely fixed, except the order by part, which is taken from a cookie. Then it simply navigates to that url by setting document.location. A small test shows you even get a csv file if you leave the order by part empty, so probably, you can just download the CSV from the base url that is in the code.

Downloading can be done using various tools, for instance WGet for Windows. See SuperUser for more possibilities. Anyway, 'step 1 to 3' is actually just a single command.

After that, you just need to parse the file. Parsing CSV files can be done using batch, and there are several examples available. I won't get into details, since you didn't provide any in your question.

PS. I'd check their terms of use before you actually implement this.

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

The button directs me to this link:

http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNAS:GMCR&region=usa&culture=en-US&cur=&order=asc

You could use the Python 3 module urllib and fetch the file, save it using the os or shutil modules, then parse it using one of the many CSV parsing modules, or by making your own.

Dodo
  • 323
  • 1
  • 8