How to get everything after last slash in a URL?

后端 未结 12 2150
心在旅途
心在旅途 2020-12-02 08:26

How can I extract whatever follows the last slash in a URL in Python? For example, these URLs should return the following:

URL: http://www.test.com/TEST1
ret         


        
相关标签:
12条回答
  • 2020-12-02 08:35

    Use urlparse to get just the path and then split the path you get from it on / characters:

    from urllib.parse import urlparse
    
    my_url = "http://example.com/some/path/last?somequery=param"
    last_path_fragment = urlparse(my_url).path.split('/')[-1]  # returns 'last'
    

    Note: if your url ends with a / character, the above will return '' (i.e. the empty string). If you want to handle that case differently, you need to strip the trailing / character before you split the path:

    my_url = "http://example.com/last/"
    # handle URL ending in `/` by removing it.
    last_path_fragment = urlparse(my_url).path.rstrip('/').split('/')[-1]  # returns 'last'
    
    0 讨论(0)
  • 2020-12-02 08:38

    Split the url and pop the last element url.split('/').pop()

    0 讨论(0)
  • 2020-12-02 08:41

    You don't need fancy things, just see the string methods in the standard library and you can easily split your url between 'filename' part and the rest:

    url.rsplit('/', 1)
    

    So you can get the part you're interested in simply with:

    url.rsplit('/', 1)[-1]
    
    0 讨论(0)
  • 2020-12-02 08:43
    url ='http://www.test.com/page/TEST2'.split('/')[4]
    print url
    

    Output: TEST2.

    0 讨论(0)
  • 2020-12-02 08:44
    extracted_url = url[url.rfind("/")+1:];
    
    0 讨论(0)
  • 2020-12-02 08:45

    First extract the path element from the URL:

    from urllib.parse import urlparse
    parsed= urlparse('https://www.dummy.example/this/is/PATH?q=/a/b&r=5#asx')
    

    and then you can extract the last segment with string functions:

    parsed.path.rpartition('/')[2]
    

    (example resulting to 'PATH')

    0 讨论(0)
提交回复
热议问题