如何检查 imgur URL 是 gif 还是图像?

How do I check whether an imgur URL is a gif or an image?

假设我有一个 imgur url 像这样:https://imgur.com/pCEoGjF

来自 url 我不知道这是图片还是 gif。我知道我可以在文件末尾添加“.jpg”以获得图像的直接 link,但如果 link 恰好是 gif,这也会显示 gif。我将如何以编程方式(使用 imgur API 或其他方式)检查 url https://imgur.com/pCEoGjFhttps://imgur.com/pCEoGjF 中的项目。 jpg是图片还是gif?

编辑: 我试过了

print(requests.head(submission.url + ".jpg").headers['Content-Type'])

但它抱怨以下错误:

KeyError: 'content-type'

暗示 content-type 不是 header 中的键。我打印了 headers 并确认了这一点,因为它们如下:

{'Content-Length': '0', 'X-Served-By': 'cache-sjc3138-SJC', 'X-Cache': 'HIT', 'Accept-Ranges': 'bytes', 'X-Timer': 'S1498627080.598287,VS0,VE0', 'Server': 'cat factory 1.0', 'Retry-After': '0', 'Connection': 'close', 'X-Cache-Hits': '0', 'Location': 'https://i.imgur.com/APMnK9t.jpg', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Date': 'Wed, 28 Jun 2017 05:17:59 GMT', 'X-Frame-Options': 'DENY'}

我想我可以检查 content-length 是否不为 0 以查看它是否是 gif?想法?

使用 requests library:

import requests

def get_content_type(url):
    return requests.head(url).headers['Content-Type']

print(get_content_type('https://i.imgur.com/pCEoGjF.jpg'))  # image/jpeg
print(get_content_type('https://i.imgur.com/kJNdeQv.jpg'))  # image/gif