可以使用rfind()来寻找字符串中最右边(即最后一次)出现'/'的位置,然后截取这个位置后一个位置到结尾所有的字符即可。
代码如下:
参考链接:
python 如何查找字符串中首次、最后一次出现的字符位置_给你两个字符串,在字符串中找出出现的第一个位置 python_779醒的博客-CSDN博客
如何查找字符串中最后一次出现的字符位置str =r'123-abc-!@#'str.rfind('-')# 返回最右边的字符位置_给你两个字符串,在字符串中找出出现的第一个位置 python
https://blog.csdn.net/chenbaixing/article/details/106848195
Python截取字符串(字符串切片)方法详解
字符串本质上就是由多个字符组成的,因此程序允许通过索引来操作字符,比如获取指定索引处的字符,获取指定字符在字符串中的位置等。 Python 字符串直接在方括号([])中使用索引
http://c.biancheng.net/view/2178.html
def getFileName(url):
# https://blog.csdn.net/chenbaixing/article/details/106848195
# 寻找网址字符串中,最后一次出现'/'的位置
index = url.rfind('/')
#print("index=",index)
# http://c.biancheng.net/view/2178.html
# 截取最后一次出现'/'的位置后一个字符,到网址结尾所有的字符
fileName= url[index+1:]
# 返回结果
return fileName
url1="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
url2="https://a.h1static.cn/bbs/2.0/template/color_free11/images/x64_logo.png"
url3="https://home.x64bbs.cn/uc_server/data/avatar/000/00/03/56_avatar_middle.jpg"
print(getFileName(url1))
print(getFileName(url2))
print(getFileName(url3))
