2012-09-18 44 views
8

我需要獲取Internet(Intranet)資源的內容類型而非本地文件。我如何從一個URL後面的資源得到MIME類型:Python:如何獲取URL的內容類型?

我嘗試這樣做:

res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry") 
http_message = res.info() 
message = http_message.getplist() 

我得到: ['charset=UTF-8']

我怎樣才能獲得Content-Type,可以做到用urllib以及如何或如果不是另一種方式?

+4

見http://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib-call – sqrtsben

+0

打印res.info ().gettype() –

+0

http://stackoverflow.com/a/21515813/538284 –

回答

15
res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry") 
http_message = res.info() 
full = http_message.type # 'text/plain' 
main = http_message.maintype # 'text' 
+2

注意:這隻適用於python 2.x –

10

一個Python3解決這個:

import urllib.request 
with urllib.request.urlopen('http://www.google.com') as response: 
    info = response.info() 
    print(info.get_content_type())  # -> text/html 
    print(info.get_content_maintype()) # -> text 
    print(info.get_content_subtype()) # -> html