How to get everything after last slash in a URL?

后端 未结 12 2151
心在旅途
心在旅途 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:46

    rsplit should be up to the task:

    In [1]: 'http://www.test.com/page/TEST2'.rsplit('/', 1)[1]
    Out[1]: 'TEST2'
    
    0 讨论(0)
  • 2020-12-02 08:48

    urlparse is fine to use if you want to (say, to get rid of any query string parameters).

    import urllib.parse
    
    urls = [
        'http://www.test.com/TEST1',
        'http://www.test.com/page/TEST2',
        'http://www.test.com/page/page/12345',
        'http://www.test.com/page/page/12345?abc=123'
    ]
    
    for i in urls:
        url_parts = urllib.parse.urlparse(i)
        path_parts = url_parts[2].rpartition('/')
        print('URL: {}\nreturns: {}\n'.format(i, path_parts[2]))
    

    Output:

    URL: http://www.test.com/TEST1
    returns: TEST1
    
    URL: http://www.test.com/page/TEST2
    returns: TEST2
    
    URL: http://www.test.com/page/page/12345
    returns: 12345
    
    URL: http://www.test.com/page/page/12345?abc=123
    returns: 12345
    
    0 讨论(0)
  • 2020-12-02 08:55

    Here's a more general, regex way of doing this:

        re.sub(r'^.+/([^/]+)$', r'\1', url)
    
    0 讨论(0)
  • 2020-12-02 08:59

    One more (idio(ma)tic) way:

    URL.split("/")[-1]
    
    0 讨论(0)
  • 2020-12-02 08:59
    os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/'))
    
    >>> folderD
    
    0 讨论(0)
  • 2020-12-02 09:01

    You can do like this:

    head, tail = os.path.split(url)
    

    Where tail will be your file name.

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