     [Answers](https://scrapfly.io/blog)   /  [python](https://scrapfly.io/blog/tag/python)   /  [How to fix Python requests TooManyRedirects error?](https://scrapfly.io/blog/answers/python-requests-exception-toomanyredirects)   # How to fix Python requests TooManyRedirects error?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Dec 19, 2022 1 min read [\#python](https://scrapfly.io/blog/tag/python) [\#requests](https://scrapfly.io/blog/tag/requests) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fpython-requests-exception-toomanyredirects "Share on LinkedIn")    

 

 

`TooManyRedirects` error can be seen when using Python [requests](https://pypi.org/project/requests/) module to scrape websites with incorrectly configured redirects:

python```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```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 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fpython-requests-exception-toomanyredirects) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fpython-requests-exception-toomanyredirects) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fpython-requests-exception-toomanyredirects) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fpython-requests-exception-toomanyredirects) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fpython-requests-exception-toomanyredirects) 



 ## Related Articles

 [  

 http 

### What is HTTP 401 Error and How to Fix it

Discover the HTTP 401 error meaning, its causes, and solutions in this comprehensive guide. Learn how 401 unauthorized e...

 

 ](https://scrapfly.io/blog/posts/what-is-http-401-error-and-how-to-fix-it) [  

 http 

### Guide to Cloudflare's Error Code 520 and How to Fix it

Quick look at error code 520, what does it mean, its common causes, and how it can be prevented.

 

 ](https://scrapfly.io/blog/posts/guide-to-error-code-520-cloudflare-and-fixes) [  

 http 

### What is HTTP 422 Error? (Unprocessable Entity)

422 Unprocessable Entity error is usually caused by a semantically invalid request. Learn http error 422 causes and how ...

 

 ](https://scrapfly.io/blog/posts/what-is-http-422-error-unprocessable-entity) 

  ## Related Questions

- [ Q How to fix python requests ConnectTimeout error? ](https://scrapfly.io/blog/answers/python-requests-exception-connecttimeout)
- [ Q How to fix Python requests MissingSchema error? ](https://scrapfly.io/blog/answers/python-requests-exception-missingschema)
- [ Q How to fix Python requests ReadTimeout error? ](https://scrapfly.io/blog/answers/python-requests-exception-readtimeout)
- [ Q How to fix Python requests SSLError? ](https://scrapfly.io/blog/answers/python-requests-exception-sllerror)
 
  



   



 Scale your web scraping effortlessly, **1,000 free credits** [Start Free](https://scrapfly.io/register)