python3中的urllib库里的重定向判断,响应码出现302为重定向,以访问百度为例:
from urllib import request class NoRedirHandler(request.HTTPRedirectHandler): def http_error_302(self, req, fp, code, msg, headers): return fp http_error_301 = http_error_302 # other_handler = ... opener = request.build_opener(NoRedirHandler) rsp = opener.open('https://baidu.com') print(rsp.code) #>> 302 #rsp.read() #>> b''
运行结果:
302
Process finished with exit code 0