How to safely get the file extension from a URL?

后端 未结 9 1227
北荒
北荒 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:04

    you can try the rfc6266 module like:

    import requests
    import rfc6266
    
    req = requests.head(downloadLink)
    headersContent = req.headers['Content-Disposition']
    rfcFilename = rfc6266.parse_headers(headersContent, relaxed=True).filename_unsafe
    filename = requests.utils.unquote(rfcFilename)
    

提交回复
热议问题