首页 > 代码库 > Python 获取URL访问的HEAD头信息

Python 获取URL访问的HEAD头信息

主要给出两个版本,一个是通过 urllib 实现的,另一个是通过 urllib2 实现的,此为第二种,代码及实例如下:

>>> import urllib2>>> from urllib2 import *>>> webUrl = "http://www.baidu.com">>> doc = urllib2.urlopen(webUrl)>>> print doc.info() #或 doc.headers

打印信息

Date: Mon, 24 Nov 2014 07:22:07 GMTContent-Type: text/html; charset=utf-8Transfer-Encoding: chunkedConnection: CloseVary: Accept-EncodingSet-Cookie: BAIDUID=6F43AAA51E609EB80D9C0A18BB11720D:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comSet-Cookie: BAIDUPSID=6F43AAA51E609EB80D9C0A18BB11720D; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comSet-Cookie: BDSVRTM=0; path=/Set-Cookie: BD_HOME=0; path=/Set-Cookie: H_PS_PSSID=7931_10161_1433_7800_9452_10120_10016_9499_10051_10065_9967_9769_9105_9757_10007_9979_9024; path=/; domain=.baidu.comP3P: CP=" OTI DSP COR IVA OUR IND COM "Cache-Control: privateCxy_all: baidu+86091fc3efbc80d96c839cee1b7a0a36Expires: Mon, 24 Nov 2014 07:21:28 GMTX-Powered-By: HPHPServer: BWS/1.1BDPAGETYPE: 1BDQID: 0xcf6e6ab100002443BDUSERID: 0

 

Python 获取URL访问的HEAD头信息