Matas Mikelionis
Matas Mikelionis

Reputation: 15

How to download a file that takes 5 seconds to finish with python?

I am trying to write some code that would download a file. Now this file from this website specifically, once you go on to that link, it takes 5 seconds for it to actually prompt the download, for example: https://sourceforge.net/projects/esp32-s2-mini/files/latest/download

I have tried using the obvious methods, such as wget.download and urllib.request.urlretrieve

urllib.request.urlretrieve('https://sourceforge.net/projects/esp32-s2-mini/files/latest/download', 'zzz')

get.download('https://sourceforge.net/projects/esp32-s2-mini/files/latest/download', 'zzzdasdas')

However, that does not work, it downloads something else, but not what I want it to.

Any suggestions would be great.

Upvotes: 0

Views: 168

Answers (1)

Lime
Lime

Reputation: 372

Using chrome's download page (ctrl+j should open it, or just click "Show All" when downloading a file), we can see all of our recent downloads. The link you provided is just the page that begins the download, not the location of the actual file itself. Right-clicking the blue name lets us copy the address to the actual file being downloaded.context menu of file from chrome downloads page The actual link of the file, in this case, is https://cfhcable.dl.sourceforge.net/project/esp32-s2-mini/ToolFlasher/NodeMCU-PyFlasher-3.0-x64.exe

We can then make a GET request to download the file. Testing this out with bash wget downloads the file properly.

wget https://versaweb.dl.sourceforge.net/project/esp32-s2-mini/ToolFlasher/NodeMCU-PyFlasher-3.0-x64.exe

You can, of course, use python requests to accomplish this as well.

import requests

response = requests.get(r"https://cfhcable.dl.sourceforge.net/project/esp32-s2-mini/ToolFlasher/NodeMCU-PyFlasher-3.0-x64.exe")

with open("NodeMCU-PyFlasher-3.0-x64.exe", "wb") as f:
    f.write(response.content)

Note that we are using wb (write bytes) mode instead of the default w (write).

Upvotes: 1

Related Questions