1 回答
TA贡献1810条经验 获得超4个赞
使用正则表达式模式 --> r"v\d+\/(.+)$". 假设随机数开头为v
前任:
import re
from urllib.parse import urlparse
ptrn = re.compile(r"v\d+\/(.+)$")
url = "https://cdn.example.com/user/image/upload/v87879798/images/profile/oaz4wkjkjsbzxa3xlkmu.jpg"
parsed_url = urlparse(url)
print(ptrn.search(parsed_url.path).group(1))
输出:
images/profile/oaz4wkjkjsbzxa3xlkmu.jpg
演示:
ptrn = re.compile(r"v\d+\/(.+)$")
urls = ["https://cdn.example.com/user/image/upload/v87879798/images/profile/oaz4wkjkjsbzxa3xlkmu.jpg", "https://cdn.example.com/user/image/upload/v87879798/images/profile/oaz4wkjkjsbzxa3xlkmu.jpg",
"https://cdn.example.com/user/image/upload/v87879798/images/oaz4wkjkjsbzxa3xlkmu.jpg", "https://cdn.example.com/user/image/upload/v87879798/oaz4wkjkjsbzxa3xlkmu.jpg"]
for url in urls:
parsed_url = urlparse(url)
print(ptrn.search(parsed_url.path).group(1))
输出:
images/profile/oaz4wkjkjsbzxa3xlkmu.jpg
images/profile/oaz4wkjkjsbzxa3xlkmu.jpg
images/oaz4wkjkjsbzxa3xlkmu.jpg
oaz4wkjkjsbzxa3xlkmu.jpg
添加回答
举报