写爬虫时如果需要获取所有的响应头,可以用如下方法:
req = urllib2.Request( 'http://xxxx');
response = urllib2.urlopen( req, timeout=5 );
#header name & body
for h in response.info().headers:
print h
print ''.join(response.info().headers)
#header name list
for h in response.headers:
print h
#get one header's body
>>> response.info().getheader('Content-Type')
'text/html;charset=UTF-8'
>>>
>>> response.headers.get('Content-Type')
'text/html;charset=UTF-8'
>>>
网友评论