gpt4 book ai didi

python - 下载文件之前如何从链接获取文件名?

转载 作者:行者123 更新时间:2023-12-01 06:32:11 25 4
gpt4 key购买 nike

例如我有a link我想在开始下载之前知道文件名。

像这样的问题有很多,但它们都归结为从链接中提取文件名。文件名不在链接中怎么办?

我找到了this solution ,但它不适用于我的情况。我始终获得传递给 urlopen() 的相同链接,而不是直接指向文件的链接。

最佳答案

对于您的示例,您可以使用以下内容:

>>> 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'

关于python - 下载文件之前如何从链接获取文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59845595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com