Python3 urllib.request does the 301/302 redirects automatically, how can you disable this behaviour?
Python3 urllib.request does the 301/302 redirects automatically, how can you disable this behaviour?
The library "requests" makes it easier, but if you need or want to use urllib.request, this works:
from urllib import request
import urllib.error
class NoRedirect(request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
opener = request.build_opener(NoRedirect)
request.install_opener(opener)
try:
r = request.urlopen('http://google.com')
except urllib.error.HTTPError as e:
r = e
print(r.status)
print(dir(r))