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