Skip to content Skip to sidebar Skip to footer

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) bu

Solution 1:

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)

Solution 2:

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:

importrequestsheaders= {
    '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)

Solution 3:

In Requests\sessions.py

change the value of self.max_redirects

You are good to go ..

Post a Comment for "Python Toomanyredirects: Exceeded 30 Redirects"