Matthew Oujiri
Matthew Oujiri

Reputation: 135

Http Error 302 in python

So while using urllib I get this error, here is the full traceback:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Dads project\Word search tool.py", line 
148, in <module>
 print_absolute_counts(line.strip('\n'), 'english', smoothing=0, 
 start_year=1799, end_year=1801)
File "C:\Users\user\Desktop\project\Word search tool.py", line 43, in 
print_absolute_counts
    absolute_counts = retrieve_absolute_counts(token, corpus, smoothing, 
start_year, end_year)
File "C:\Users\user\Desktop\Dads project\Word search tool.py", line 85, in 
retrieve_absolute_counts
   page = urllib.request.urlopen(urllib.request.Request(url)).read()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37- 
 32\lib\urllib\request.py", line 222, in urlopen
   return opener.open(url, data, timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python37- 
32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37- 
  32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
 File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 745, in http_error_302
   self.inf_msg + msg, headers, fp)
urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:

Found I want to know if their is a way to fix this, it comes as I search something in a website and it returns this, other searches appear to work perfectly, but this one specific search being: aaliis returns an error, its a sort of word search for the google ngrams.

Upvotes: 0

Views: 1597

Answers (1)

RafalS
RafalS

Reputation: 6334

HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.

Http response code 302 is for redirecting. In your case it seems like the url you requested responds with 302 too many times, so the urllib raises exception to prevent you from being stuck in infinite loop of redirections.

Probably the url you request requires some cookies and redirects you if they're not set. Add cookies to your request or use better libs that do this by default: http://docs.python-requests.org/en/master/

Upvotes: 1

Related Questions