urllib: Get name of file from direct download link

2019-05-18 17:07发布

问题:

Python 3. Probably need to use urllib to do this,

I need to know how to send a request to a direct download link, and get the name of the file it attempts to save.

(As an example, a KSP mod from CurseForge: https://kerbal.curseforge.com/projects/mechjeb/files/2355387/download)

Of course, the file ID (2355387) will be changed. It could be from any project, but always on CurseForge. (If that makes a difference on the way it's downloaded.)

That example link results in the file:

How can I return that file name in Python?

Edit: I should note that I want to avoid saving the file, reading the name, then deleting it if possible. That seems like the worst way to do this.

回答1:

Using urllib.request, when you request a response from a url, the response contains a reference to the url you are downloading.

>>> from urllib.request import urlopen    
>>> url = 'https://kerbal.curseforge.com/projects/mechjeb/files/2355387/download'
>>> response = urlopen(url)
>>> response.url
'https://addons-origin.cursecdn.com/files/2355/387/MechJeb2-2.6.0.0.zip'

You can use os.path.basename to get the filename:

>>> from os.path import basename
>>> basename(response.url)
'MechJeb2-2.6.0.0.zip'