2017年8月7日 星期一

[Python 文章收集] urllib2 - How to get the Content-Type of an URL?

Source From Here 
Question 
I need to get the content-type of an internet(intranet) resource not a local file. How can I get the MIME type from a resource behind an UR? 

How-To 
You can leverage urllib2 packages and retrieve information from HTTPMessage object: 
>>> import urllib2 
>>> res = urllib2.urlopen('http://www.kimo.com.tw') 
>>> http_message = res.info() 
>>> http_message.__class__ 
 
>>> http_message.type 
'text/html' 
>>> http_message.maintype 
'text'


沒有留言:

張貼留言

[ FP In Python ] Ch1. (Avoiding) Flow Control

Preface   In typical imperative Python programs—including those that make use of classes and methods to hold their imperative code—a block...