Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TooManyRedirects: Exceeded 30 redirects

I have tried the following code in Python

url="http://www.realtor.com/realestateandhomes-search/Pittsburgh_PA/type-single-family-home/price-na-30000/sby-1/"
r=requests.get(url)

but it throws the eror

  File "C:\Users\Dana\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\requests\sessions.py", line 630, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "C:\Users\Dana\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\requests\sessions.py", line 630, in <listcomp>
    history = [resp for resp in gen] if allow_redirects else []
  File "C:\Users\Dana\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\requests\sessions.py", line 111, in resolve_redirects
    raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects, respon
se=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

Any help would be greatly appreciated

like image 738
Andrei Avatar asked Feb 14 '17 22:02

Andrei


3 Answers

It simply means that your request got a response which was a redirect (an information that the page you were trying to reach is now located at a new spot). The requests library understands this per default and does not return this result but tries another request for the new location. Which again returned a redirect, etc.

To avoid never coming out of the requests call, there is a limit implemented for the number of redirects allowed before the process is aborted.

I assume there is an error on the site you are trying to request something from, probably a circular redirect.

You can tweak the requests library to not follow the redirects but instead return them, then you will not get this error (but of course redirect responses):

response = requests.get(url, allow_redirects=False)
like image 73
Alfe Avatar answered Oct 26 '22 18:10

Alfe


Occasionally, not often, this can happen if you do not include the headers the server is expecting. If you mimic the headers, payload, user-agent, etc. with the additional options available in requests.get() you'll be less likely to get this error.

Example:

import requests

headers = {
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.8',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
}

requests.get('http://www.realtor.com/realestateandhomes-search/Pittsburgh_PA/type-single-family-home/price-na-30000/sby-1', headers=headers)
like image 21
B.Adler Avatar answered Oct 26 '22 18:10

B.Adler


UPDATE IF YOU ARE HERE As of June 10th 2022:

IT Seems that PyPi is having an issue which is the root cause of the problem

enter image description here

Source: https://status.python.org/

like image 22
Amir Afianian Avatar answered Oct 26 '22 17:10

Amir Afianian