简体   繁体   中英

Identify the file extension of a URL

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 eg .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 (ie 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. (ie cope with cases when the URL does not actually have an extension).

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")
''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM