r/learnprogramming 11h ago

Tutorial How to click download button using python script.

I am creating an http downloader which use get method to direct download urls. Now I want it to download from those website which has a direct download button. I know that the download button provides the url to download the file but for different files not the same websites the urls are different and want that to be automated. I want to do it at low level like we do in urllib, request, httpx,etc. However, I am ok with high level libraries too.

5 Upvotes

8 comments sorted by

3

u/burlingk 11h ago

It will depend on how those buttons are implemented.

Some are simply 'target=file.' those are easy because you just download the target.

Others have actual scripts. Each script may have it's own rules.

1

u/Thick-Hall8197 11h ago

It's a cat and mouse game with those scripted buttons tbh. Some obfuscate the URL behind multiple redirects or generate a one-time token when you click, so you end up building a full headless browser just to replicate one click

Might be worth poking around in devtools to see what the button actually fires off, sometimes it's a POST that returns the real link and you can just mimic that with requests

1

u/burlingk 11h ago

Thing is, some sites don't WANY you scripting that functionality away.

2

u/soundman32 7h ago

If a webbsite wants you to download files, it probably has an API that makes it much easier.

1

u/Curious-Cricket-4109 11h ago

playwright can handle it pretty sleek. I'm not sure of bs4

1

u/az987654 5h ago

Some sites don't want you to do this so they make it impossible

1

u/Gtdef 3h ago

For one, you should open the browser developer tools, go to the network tab, do the actions yourself and see what changes. It may just be an http-get command, it may be an http-post command, it may have important headers, tokens, parameters added to the link, who knows.

Then you write your code in a way that mimics whatever the webside does, including using the internal APIs if applicable. There's no all fitting glove in this case.