python仅使用urllib2获取标头
问题内容:
我必须使用urllib2实现仅获取标头的功能(不执行GET或POST)。这是我的功能:
def getheadersonly(url, redirections = True):
if not redirections:
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
http_error_301 = http_error_303 = http_error_307 = http_error_302
cookieprocessor = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor)
urllib2.install_opener(opener)
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
info = {}
info['headers'] = dict(urllib2.urlopen(HeadRequest(url)).info())
info['finalurl'] = urllib2.urlopen(HeadRequest(url)).geturl()
return info
使用回答这个和这个的代码。但是,即使标志为,这
也会进行 重定向False
。我尝试使用以下代码:
print getheadersonly("http://ms.com", redirections = False)['finalurl']
print getheadersonly("http://ms.com")['finalurl']
问题答案:
首先,您的代码包含几个错误:
-
根据您的每个请求,
getheadersonly
安装一个新的全局urlopener,然后将其用于后续的urllib2.urlopen
-
您进行两个HTTP请求以获取响应的两个不同属性。
-
的实现
urllib2.HTTPRedirectHandler.http_error_302
并不是那么简单,而且我不了解它最初如何防止重定向。
基本上,您应该了解每个处理程序都安装在打开程序中以处理某种响应。urllib2.HTTPRedirectHandler
是否可以将某些HTTP代码转换为重定向。如果您不希望重定向,请不要在重定向器中添加重定向处理程序。如果您不想打开ftp链接,请不要添加FTPHandler
,等等。
urllib2.HTTPHandler()
要做的就是创建一个新的打开器并将其添加到其中,将请求自定义为“
HEAD”请求,然后将请求的实例传递给打开器,读取属性,然后关闭响应。
class HeadRequest(urllib2.Request):
def get_method(self):
return 'HEAD'
def getheadersonly(url, redirections=True):
opener = urllib2.OpenerDirector()
opener.add_handler(urllib2.HTTPHandler())
opener.add_handler(urllib2.HTTPDefaultErrorHandler())
if redirections:
# HTTPErrorProcessor makes HTTPRedirectHandler work
opener.add_handler(urllib2.HTTPErrorProcessor())
opener.add_handler(urllib2.HTTPRedirectHandler())
try:
res = opener.open(HeadRequest(url))
except urllib2.HTTPError, res:
pass
res.close()
return dict(code=res.code, headers=res.info(), finalurl=res.geturl())