Python split url to find image name and extension

后端 未结 7 1964
逝去的感伤
逝去的感伤 2021-02-04 13:43

I am looking for a way to extract a filename and extension from a particular url using Python

lets say a URL looks as follows

picture_page = \"http://dis         


        
相关标签:
7条回答
  • 2021-02-04 14:20

    Try with urlparse.urlsplit to split url, and then os.path.splitext to retrieve filename and extension (use os.path.basename to keep only the last filename) :

    import urlparse
    import os.path
    
    picture_page = "http://distilleryimage2.instagram.com/da4ca3509a7b11e19e4a12313813ffc0_7.jpg"
    
    print os.path.splitext(os.path.basename(urlparse.urlsplit(picture_page).path))
    
    >>> ('da4ca3509a7b11e19e4a12313813ffc0_7', '.jpg')
    
    0 讨论(0)
提交回复
热议问题