I will write about methods to correctly download binaries from URLs and set their filenames. What do you think will happen if the above code is used to download it? If you said that a HTML page will be downloaded, you are spot on. This was one of the problems I faced in the Import module of Open Event where I had to download media from certain links. When the URL linked to a webpage rather than a binary, I had to not download that file and just keep the link as is.
To solve this, what I did was inspecting the headers of the URL. Headers usually contain a Content-Type parameter which tells us about the type of data the url is linking to. A naive way to do it will be -. It works but is not the optimum way to do so as it involves downloading the file for checking the header.
So if the file is large, this will do nothing but waste bandwidth. I looked into the requests documentation and found a better way to do it. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. How to create in-memory file object Ask Question. Asked 4 years, 5 months ago. Active 7 months ago. Viewed 80k times. Add a comment. Active Oldest Votes. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. Podcast Who is building clouds for the independent developer? Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Looks like it should have. I find requests to be less finicky.
Use the example above. Sai Sai 1 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
0コメント