How to safely get the file extension from a URL?

后端 未结 9 1222
北荒
北荒 2021-02-02 08:40

Consider the following URLs

http://m3u.com/tunein.m3u
http://asxsomeurl.com/listen.asx:8024
http://www.plssomeotherurl.com/station.pls?id=111
http://22.198.133.16:802         


        
9条回答
  •  [愿得一人]
    2021-02-02 09:16

    Use urlparse, that'll get most of the above sorted:

    http://docs.python.org/library/urlparse.html

    then split the "path" up. You might be able to split the path up using os.path.split, but your example 2 with the :8024 on the end needs manual handling. Are your file extensions always three letters? Or always letters and numbers? Use a regular expression.

提交回复
热议问题