0

Is there a way to download a CSV file named "EXPE Key Ratios.csv" inside below link using Python?

http://financials.morningstar.com/ratios/r.html?t=EXPE&region=usa&culture=en-US

Without Python, this can be easily downloaded by clicking on the "Export" button but I have no Javascript knowledge and I do not know how to generate the real download path in Python by chasing down the JS code. I tried to follow the steps in this post, but I am unable to get this tailored to my problem. Any help is appreciated.

mhsnk
  • 97
  • 1
  • 6

2 Answers2

1

No selenium needed, what was missing is the Referer header. The javascript function simply appends the order method to http://financials.morningstar.com/finan/ajax/exportKR2CSV.html?&callback=?&t=XNAS:EXPE&region=usa&culture=en-US&cur=&order= and the default one is asc.

import requests

headers = {
'Referer': 'http://financials.morningstar.com/ratios/r.html?t=EXPE&region=usa&culture=en-US',
}

r = requests.get("http://financials.morningstar.com/finan/ajax/exportKR2CSV.html?&callback=?&t=XNAS:EXPE&region=usa&culture=en-US&cur=&order=asc", headers=headers)

csv = r.content

with open("EXPE Key Ratios.csv", "wb") as file:
    file.write(csv)
Shar
  • 448
  • 3
  • 8
0

Ok, so I have been working on this, and the closest I have come is getting it into your downloads folder -- I have no clue how this website is interacting with the browser to invoke a direct download like this. If someone more knowledgeable on the back-end of all of this could explain how it works I would greatly appreciate it.

Anyways, here is my code for getting it to the downloads folder:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#Get a driver
driver = webdriver.Edge()

#Navigate to the page
driver.get('http://financials.morningstar.com/ratios/r.html?t=EXPE&region=usa&culture=en-US')

#This is the javascript function that is invoked.
driver.execute_script("exportKeyStat2CSV()")

#Close the browser
driver.quit()

This should place the csv in your downloads folder (wherever that is for edge).

To install the webdriver go here, download it and put it in the directory with your script. You will have to pip install selenium.

Astormooke
  • 71
  • 7