python httplib/urllib获取文件名 [英] python httplib/urllib get filename

查看:271
本文介绍了python httplib/urllib获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获取文件名

e.g. xyz.com/blafoo/showall.html

如果您使用urllib或httplib?

if you work with urllib or httplib?

以便我可以将文件保存在服务器上的文件名下?

so that i can save the file under the filename on the server?

如果您访问

xyz.com/blafoo/ 

您看不到文件名.

谢谢

推荐答案

要从响应http头获取文件名:

To get filename from response http headers:

import cgi

response = urllib2.urlopen(URL)
_, params = cgi.parse_header(response.headers.get('Content-Disposition', ''))
filename = params['filename']

要从URL获取文件名:

To get filename from the URL:

import posixpath
import urlparse 

path = urlparse.urlsplit(URL).path
filename = posixpath.basename(path)

这篇关于python httplib/urllib获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆