简体   繁体   中英

Download a file using python from a webpage without opening the webpage

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.

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM