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
Use urllib.request.Request
:
import urllib
req = urllib.request.Request(url, method='HEAD')
r = urllib.request.urlopen(req)
print(r.info().get_filename())
Example :
In[1]: urllib.request.urlopen(urllib.request.Request('https://httpbin.org/response-headers?content-disposition=%20attachment%3Bfilename%3D%22example.csv%22', method='HEAD')).info().get_filename()
Out[1]: 'example.csv'
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)
Does not make much sense what you are asking. The only thing that you have is the URL. Either extract the last part from the URL or you may check the HTTP response for something like
content-disposition: attachment;filename="foo.bar"
This header can be set by the server to indicate that the filename is foo.bar. This is usually used for file downloads or something similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With