如何获取URL中最后一个斜杠之后的所有内容?

问题:如何获取URL中最后一个斜杠之后的所有内容?

如何提取Python中URL中最后一个斜杠之后的内容?例如,这些URL应该返回以下内容:

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

我已经尝试过urlparse,但这给了我完整的路径文件名,例如page/page/12345

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
returns: TEST1

URL: http://www.test.com/page/TEST2
returns: TEST2

URL: http://www.test.com/page/page/12345
returns: 12345

I’ve tried urlparse, but that gives me the full path filename, such as page/page/12345.


回答 0

您不需要花哨的东西,只需在标准库中查看字符串方法,就可以轻松地在“ filename”部分和其余部分之间拆分url:

url.rsplit('/', 1)

因此,您可以简单地通过以下方式获得您感兴趣的部分:

url.rsplit('/', 1)[-1]

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]

回答 1

另一种(惯用的)方式:

URL.split("/")[-1]

One more (idio(ma)tic) way:

URL.split("/")[-1]

回答 2

rsplit 应该完成任务:

In [1]: 'http://www.test.com/page/TEST2'.rsplit('/', 1)[1]
Out[1]: 'TEST2'

rsplit should be up to the task:

In [1]: 'http://www.test.com/page/TEST2'.rsplit('/', 1)[1]
Out[1]: 'TEST2'

回答 3

您可以这样:

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

其中tail是您的文件名。

You can do like this:

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

Where tail will be your file name.


回答 4

如果需要,可以使用urlparse(例如,摆脱任何查询字符串参数)。

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]))

输出:

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

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

回答 5

os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/'))
>>> folderD
os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/'))
>>> folderD

回答 6

这是更通用的正则表达式方法:

    re.sub(r'^.+/([^/]+)$', r'\1', url)

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

    re.sub(r'^.+/([^/]+)$', r'\1', url)

回答 7

extracted_url = url[url.rfind("/")+1:];
extracted_url = url[url.rfind("/")+1:];

回答 8

partition并且rpartition对于此类事情也很方便:

url.rpartition('/')[2]

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')


回答 9

分割网址并弹出最后一个元素 url.split('/').pop()

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


回答 10

url ='http://www.test.com/page/TEST2'.split('/')[4]
print url

输出:TEST2

url ='http://www.test.com/page/TEST2'.split('/')[4]
print url

Output: TEST2.