I need to extract file extension from Three type of direct and indirect URL.个
"https://needmode.com/products/350e0f54c3480dc035d6db5e7ef898711d5f4ebc_1683455668.jpg"
"https://dkstatics-public.digikala.com/digikala-products/350e0f54c3480dc035d6db5e7ef898711d5f4ebc_1683455668.jpg?x-oss-process=image/resize,m_lfit、h_800、w_800/质量、Q_90"
"https://meghdadit.com/_image.ashx?i=%252ffiles%252fproduct%252f4778c8kbqjb7k18sqydnkztp4yzi0jlaug5j5jtybsmuw0lzq2%255blarge%255d.jpg"
我的目标是在所有类型的URL中返回"jpg"作为文件扩展名.
我的python代码如下:
from urllib.parse import urlparse
import os
img = "IMAGE URL"
parsed_url = urlparse(img)
filename_and_extension = parsed_url.path.rsplit("/", maxsplit=1)[-1]
file_extension = parsed_url.path.rsplit(".", maxsplit=1)[-1].lower()
print("first method: "+file_extension)
filename, file_extension = os.path.splitext(img)
print("second method: "+file_extension)
第一种方法不适用于第三个URL,第二种方法不适用于第二个URL.
有没有办法对从URL右侧 Select 扩展名的第一种方法进行优先排序?