欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python获取url文件名_python httplib / urllib获取文件名

发布时间:2025/3/21 python 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python获取url文件名_python httplib / urllib获取文件名 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

is there a possibillity to get the filename

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

if you work with urllib or httplib?

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

if you go to sites like

xyz.com/blafoo/

you cant see the filename.

Thank you

解决方案

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

To get filename from the URL:

import posixpath

import urlparse

path = urlparse.urlsplit(URL).path

filename = posixpath.basename(path)

总结

以上是生活随笔为你收集整理的python获取url文件名_python httplib / urllib获取文件名的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。