Logo New Black

Troubleshooting Python Requests Exception TooManyRedirects: A Comprehensive Guide

When using the Python requests module to scrape websites, you may encounter a TooManyRedirects error. This error is typically triggered by a request that is redirected too many times, exceeding the maximum limit set by the requests library. Such issues often stem from improperly configured website redirects or infinite loop scenarios, which can halt your scraping process. To circumvent these hurdles and ensure a smoother web scraping experience, leveraging a robust web scraping API can be incredibly beneficial. These APIs are engineered to handle complex web navigation patterns, including extensive redirects, thereby facilitating efficient and effective data extraction. By incorporating a high-quality web scraping API into your projects, you can minimize disruptions caused by TooManyRedirects and other common web scraping challenges.

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 typically indicates one of three scenarios:

  • The website is not configured correctly.
  • Our requests lack crucial details such as headers or cookies.
  • The scraper is intentionally redirected in a loop to deter scraping (i.e., blocking).

To manage the TooManyRedirects exception, we should disable automatic redirects and handle them manually:

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)

Alternatively, to avoid all of the Cloudflare errors, consider using web scraping APIs, such as those offered by Scrape Network.