Python Find Question

2019-07-20 17:53发布

I am using Python to extract the filename from a link using rfind like below:

url = "http://www.google.com/test.php"

print url[url.rfind("/") +1 : ]

This works ok with links without a / at the end of them and returns "test.php". I have encountered links with / at the end like so "http://www.google.com/test.php/". I am have trouble getting the page name when there is a "/" at the end, can anyone help?

Cheers

标签: python url
7条回答
乱世女痞
2楼-- · 2019-07-20 18:42

Use [r]strip to remove trailing slashes:

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

If a wider range of possible URLs is possible, including URLs with ?queries, #anchors or without a path, do it properly with urlparse:

path= urlparse.urlparse(url).path
return path.rstrip('/').rsplit('/', 1)[-1] or '(root path)'
查看更多
登录 后发表回答