Search code examples
pythonpython-3.xhttp-redirectpython-requestsurllib

How to get the file name from a link before downloading the file?


For example I have a link and I want to know the file name before I start the download.

There are many questions like this, but they all boil down to extracting the file name from the link. What should I do when file name is not in the link?

I found this solution, but it is not working in my situation. I keep get the same link I passed to urlopen() instead of the direct link to the file.


Solution

  • For your example, you can use the following:

    >>> from urllib.request import urlopen
    >>> url = 'http://booksdl.org/get.php?md5=5c9afff27ee041c34c698d62bd510616&key=D3JE34WPUY7LQ11U'
    >>> response = urlopen(url)
    >>> response.headers.get_filename()
    '[Progress in Mathematics 244] Michael Atiyah (auth.), Pavel Etingof, Vladimir Retakh, I. M. Singer (eds.) - The Unity of Mathematics_ In Honor of the Ninetieth Birthday of I.M. Gelfand (2006, Birkhäuser Basel).pdf'