This comment has been minimized. Sign in to view. Copy link Quote reply. Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window.
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.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Download bulk images in python Ask Question. Asked 11 months ago. Active 11 months ago. Viewed times. After watching a video about how to download images using python, I typed the code in the video and here's the code import pandas as pd import urllib. Improve this question. YasserKhalil YasserKhalil 5, 3 3 gold badges 18 18 silver badges 48 48 bronze badges.
It seems like you need to decode first. Does this answer your question? Thanks a lot. The code is working now but why some links don't work? Well, as stated, it seems like it's forbidden.
0コメント