🚀 We are hiring! See open positions

How to fix Python requests TooManyRedirects error?

by Bernardas Alisauskas Jun 15, 2023 1 min read

TooManyRedirects error can be seen when using Python requests module to scrape websites with incorrectly configured redirects:

python
import requests

requests.get("https://httpbin.dev/redirect/31")  # default redirect limit is 30
# will raise:
# TooManyRedirects(requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

# we can set max redirects using requests.Session:
session = requests.Session()
session.max_redirects = 2
session.get("https://httpbin.dev/redirect/3")

When web scraping, this usually means one of 3 things:

  • The website is incorrectly configured.
  • Our requests are missing important details like headers or cookies.
  • The scraper is purposefully redirected in a loop to prevent scraping (i.e. blocking).

To handle ToomanyRedirects exception we should disable automatic redirects and handle them manually:

python
import requests

session = requests.Session()
response = session.get("https://httpbin.dev/redirect/3", allow_redirects=False)
redirect_url = response.headers['Location']
# now we can manually inspect and fix the redirect url if necessary and then follow it:
response2 = session.get(redirect_url, allow_redirects=False)
Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.