Python中的HTTP错误302

时间:2018-06-20 18:22:59

标签: python urllib

因此在使用urllib时出现此错误,这是完整的回溯:

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:

发现 我想知道他们是否是解决此问题的一种方式,它是在我在网站上搜索某些内容并返回此错误时出现的,其他搜索似乎效果很好,但是这种特定搜索是:aaliis返回错误,这是一种错误单词搜索Google ngrams。

1 个答案:

答案 0 :(得分:1)

  

HTTP错误302:HTTP服务器返回的重定向错误将导致无限循环。

Http响应代码302用于重定向。在您的情况下,您请求的网址似乎响应302次太多,因此urllib引发了异常,以防止您陷入无限的重定向循环中。

您请求的网址可能需要一些cookie,如果未设置它们,则会将您重定向。将Cookie添加到您的请求中,或使用更好的库来默认执行以下操作:http://docs.python-requests.org/en/master/