Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何获取URL的内容类型?_Python_Python 2.7_Urllib - Fatal编程技术网

Python:如何获取URL的内容类型?

Python:如何获取URL的内容类型?,python,python-2.7,urllib,Python,Python 2.7,Urllib,我需要获取internet(intranet)资源的内容类型,而不是本地文件。如何从URL后面的资源获取MIME类型: 我试过这个: res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry") http_message = res.info() message = http_message.getplist() 我得到: ['charset=UTF-8'] 如何获取内容类型,可以使用urlli

我需要获取internet(intranet)资源的内容类型,而不是本地文件。如何从URL后面的资源获取MIME类型:

我试过这个:

res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry")
http_message = res.info()
message = http_message.getplist()
我得到:
['charset=UTF-8']


如何获取
内容类型
,可以使用
urllib
完成,如果没有,如何获取

Python3解决方案:

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'
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
看见