我正在尝试使用Python 2对页面执行HEAD请求。
我在努力import misc_urllib2
.....
opender = urllib2.build_opener([misc_urllib2.MyHTTPRedirectHandler(), misc_urllib2.HeadRequest()])
含有misc_urllib2.py的class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def __init__ (self):
self.redirects = []
def http_error_301(self, req, fp, code, msg, headers):
result = urllib2.HTTPRedirectHandler.http_error_301(
self, req, fp, code, msg, headers)
result.redirect_code = code
return result
http_error_302 = http_error_303 = http_error_307 = http_error_301
但是我得到了TypeError: __init__() takes at least 2 arguments (1 given)
如果我愿意opender = urllib2.build_opener(misc_urllib2.MyHTTPRedirectHandler())
那就行了