We can also specify the HTTP methods to download the file. Reference : Requests Module Official Docs. Your email address will not be published. Pankaj I love Open Source technologies and writing about my experience about them is my passion. Follow Author. Comments Rajeev Sharma says:. February 13, at pm. August 5, at am. Task says:. June 24, at am. Leave a Reply Cancel reply Your email address will not be published. Leave this field empty. Exact matches only. Search in title.
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.
That way involved just fetching the headers of a url before actually downloading it. This allows us to skip downloading files which weren't meant to be downloaded. To restrict download by file size, we can get the filesize from the Content-Length header and then do suitable comparisons. We can parse the url to get the filename. This will be give the filename in some cases correctly. However, there are times when the filename information is not present in the url. In that case, the Content-Disposition header will contain the filename information.
Here is how to fetch it. The url-parsing code in conjuction with the above method to get filename from Content-Disposition header will work for most of the cases.
Use them and test the results. These are my 2 cents on downloading files using requests in Python. Let me know of other tricks I might have overlooked.
This article was first posted on my personal blog. Especially if the files are big. That is good idea and using with as a context manager is more better and looks great.
0コメント