0

I was looking for a way to write a script that will download a file from a specific website but without opening the website itself. I want everything to happen in the background.

The website is morningstar and a specific link for the example is this one: https://financials.morningstar.com/ratios/r.html?t=MSFT in this page, there is a "Button" (it is not really declared as a button but as a Hyperlink, the <a> tag in HTML)

I added a photo in the bottom so you can see for yourself exactly the way they wrote the code.

Anyway, I saw that when I clicked the button the href attribute actually calls a javascript function which then creates the links from which the file will be downloaded.

I am looking for a way that I can write a script and give it the link I want, for example, the link above, and the script will download this specific CSV file from that page into a folder of my choice.

I was looking at some selenium tutorials but I couldn't find much help for my specific problem.

enter image description here

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
BenGababy
  • 11
  • 2
  • What do you mean by "without opening the webpage"? Why is this important? – ChrisGPT was on strike Jan 15 '20 at 23:19
  • Hi perhaps PhantomJS might work in this case? https://stackoverflow.com/a/26440563/11746212 – IronMan Jan 15 '20 at 23:21
  • If you know the url of the download you can use [requests](https://likegeeks.com/downloading-files-using-python/) – K753 Jan 15 '20 at 23:26
  • @K753 only if the site is static, but I'm reasonably certain morningstar uses dynamic scripting – G. Anderson Jan 15 '20 at 23:30
  • have you tried their API? not sure if its free but here is a link. https://developer.morningstar.com/apis/investment-screener-api-us?regions=350#GettingStarted – Randy Maldonado Jan 15 '20 at 23:55
  • @Chris Well, that is not the most important thing but I would like for the code to work cleaner and let's say I would like to download 20 different files on the same run of the program. that will open a 20 different browser's tabs, wouldn't it? just seems not so clean... – BenGababy Jan 16 '20 at 00:20

1 Answers1

0

Here's an example I use:

import requests

url = 'http://via.placeholder.com/350x150'

dashboardFile = requests.get(url, allow_redirects=True)

open('d:/dev/projects/new-wave/dashboard.pdf', 'wb').write(dashboardFile .content)

Oh, and depending on the size of the file you would want to download in chunks. A quick search of: "python download large file in chunks" will help.

Thomas Cayne
  • 1,132
  • 8
  • 15
  • Thank you very much for the answer, I would search a little bit about requests in python. I just uploaded the photo I forgot to post. So I don't know if that would work what you suggested but I would look into it. Thank you again. – BenGababy Jan 16 '20 at 00:23
  • You are welcome. Here's a link on SO: https://stackoverflow.com/a/16696317/4208845 – Thomas Cayne Jan 16 '20 at 00:32