Reputation: 5565
I am looking to extract the file extension if it exists for web addresses (trying to identify which links are to a list of extensions which I do not want e.g. .jpg
, .exe
etc).
So, I would want to extract from the following URL www.example.com/image.jpg
the extension jpg
, and also handle cases when there is no extension such as www.example.com/file
(i.e. return nothing).
I can't think how to implement it, but one way I thought of was to get everything after the last dot, which if there was an extension would allow me to look that extension up, and if there wasn't, for the example www.example.com/file
it would return com/file
(which given is not in my list of excluded file-extensions, is fine).
There may be an alternative superior way using a package I am not aware of, which could identify what is/isn't an actual extension. (i.e. cope with cases when the URL does not actually have an extension).
Upvotes: 3
Views: 6527
Reputation: 679
If you have no extension in URL, you can use response 'Content-Type' headers to get an extension, like so:
from urllib.request import urlopen
get_ext(url):
resp = urlopen(url)
ext = resp.info()['Content-Type'].split("/")[-1]
return ext
Upvotes: 0
Reputation: 59128
The urlparse
module (urllib.parse
in Python 3) provides tools for working with URLs. Although it doesn't provide a way to extract the file extension from a URL, it's possible to do so by combining it with os.path.splitext
:
from urlparse import urlparse
from os.path import splitext
def get_ext(url):
"""Return the filename extension from url, or ''."""
parsed = urlparse(url)
root, ext = splitext(parsed.path)
return ext # or ext[1:] if you don't want the leading '.'
Example usage:
>>> get_ext("www.example.com/image.jpg")
'.jpg'
>>> get_ext("https://www.example.com/page.html?foo=1&bar=2#fragment")
'.html'
>>> get_ext("https://www.example.com/resource")
''
Upvotes: 8