如何在URL中的最后一个斜杠之后获取所有内容? [英] How to get everything after last slash in a URL?
本文介绍了如何在URL中的最后一个斜杠之后获取所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何提取Python中URL中最后一个斜杠之后的内容?例如,这些URL应该返回以下内容:
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
我尝试了urlparse,但这给了我完整的路径文件名,例如page/page/12345
.
I've tried urlparse, but that gives me the full path filename, such as page/page/12345
.
推荐答案
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]
这篇关于如何在URL中的最后一个斜杠之后获取所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文