如何在下载文件之前从 link 中获取文件名?
How to get the file name from a link before downloading the file?
例如我有 a link,我想在开始下载之前知道文件名。
类似这样的问题很多,归根结底都是从link中提取文件名。文件名不在link中怎么办?
我找到了 ,但它在我的情况下不起作用。我一直得到相同的 link 我传递给 urlopen()
而不是直接 link 到文件。
对于您的示例,您可以使用以下内容:
>>> 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'
例如我有 a link,我想在开始下载之前知道文件名。
类似这样的问题很多,归根结底都是从link中提取文件名。文件名不在link中怎么办?
我找到了 urlopen()
而不是直接 link 到文件。
对于您的示例,您可以使用以下内容:
>>> 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'