python urllib2 获取所有响应头(response

2019-01-31  本文已影响0人  flamexyz

写爬虫时如果需要获取所有的响应头,可以用如下方法:

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'
>>>
上一篇下一篇

猜你喜欢

热点阅读