urllib2文件名

时间:2020-03-06 15:02:02  来源:igfitidea点击:

如果我使用urllib2打开文件,如下所示:

remotefile = urllib2.urlopen('http://example.com/somefile.zip')

除了解析原始URL之外,是否有一种简单的方法来获取文件名?

编辑:将openfile更改为urlopen ...不确定如何发生。

EDIT2:我最终使用:

filename = url.split('/')[-1].split('#')[0].split('?')[0]

除非我弄错了,否则这也应该消除所有潜在的查询。

解决方案

你是说urllib2.urlopen吗? urllib2模块中没有名为openfile的函数。

无论如何,使用urllib2.urlparse函数:

>>> from urllib2 import urlparse
>>> print urlparse.urlsplit('http://example.com/somefile.zip')
('http', 'example.com', '/somefile.zip', '', '')

瞧。

我认为在HTTP传输方面,"文件名"不是一个很好定义的概念。服务器可能(但不是必需)提供一个作为" content-disposition"标头,我们可以尝试通过remotefile.headers ['Content-Disposition']获得标头。如果失败,则可能必须自己解析URI。

我们是说urllib2.urlopen吗?

如果服务器通过检查remotefile.info()['Content-Disposition']来发送Content-Disposition标头,则可能会取消预期的文件名,但是因为我认为我们只需要解析url。

我们可以使用urlparse.urlsplit,但是如果我们有第二个示例中所示的URL,则最终还是必须自己拉出文件名:

>>> urlparse.urlsplit('http://example.com/somefile.zip')
('http', 'example.com', '/somefile.zip', '', '')
>>> urlparse.urlsplit('http://example.com/somedir/somefile.zip')
('http', 'example.com', '/somedir/somefile.zip', '', '')

也可以这样做:

>>> 'http://example.com/somefile.zip'.split('/')[-1]
'somefile.zip'
>>> 'http://example.com/somedir/somefile.zip'.split('/')[-1]
'somefile.zip'

我想这取决于我们通过解析的意思。如果不解析URL,就无法获取文件名,也就是说,远程服务器不会为我们提供文件名。但是,我们不必自己做很多事情,这里有urlparse模块:

In [9]: urlparse.urlparse('http://example.com/somefile.zip')
Out[9]: ('http', 'example.com', '/somefile.zip', '', '', '')

import os,urllib2
resp = urllib2.urlopen('http://www.example.com/index.html')
my_url = resp.geturl()

os.path.split(my_url)[1]

# 'index.html'

这不是openfile,但可能仍然有帮助:)

从来没听说过。

但是我们可以这样简单地解析它:

代码数量不匹配