import urllibimport mimetypesdef guess_type_of(link, strict=True): link_type, _ = mimetypes.guess_type(link) if link_type is None and strict: u = urllib.urlopen(link) link_type = u.headers.gettype() # or using: u.info().gettype() return link_type
演示:
links = ['http://stackoverflow.com/q/21515098/538284', # It's a html page 'http://upload.wikimedia.org/wikipedia/meta/6/6d/Wikipedia_wordmark_1x.png', # It's a png file 'http://commons.wikimedia.org/wiki/File:Typing_example.ogv', # It's a html page 'http://upload.wikimedia.org/wikipedia/commons/e/e6/Typing_example.ogv' # It's an ogv file]for link in links: print(guess_type_of(link))
输出:
text/htmlimage/x-pngtext/htmlapplication/ogg
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)