How to avoid / prevent Max Redirects query error in Python?

Trying to avoid Number of Redirects Exceeds 30 with python-requests . Is there something I can do to avoid this, or is it a pure server side? I got this error while trying to get facebook.com .

Thank you very much!

+4
source share
1 answer

The server redirects you in a loop. You need to find out why you are being redirected so often.

You can try to figure out what happens without allowing redirects (set allow_redirects=False ) and then resolving them using the .resolve_redirects() API :

 >>> import requests >>> url = 'http://httpbin.org/redirect/5' # redirects 5 times >>> session = requests.session() >>> r = session.get(url, allow_redirects=False) >>> r.headers.get('location') '/redirect/4' >>> for redirect in session.resolve_redirects(r, r.request): ... print redirect.headers.get('location') ... /redirect/3 /redirect/2 /redirect/1 /get None 

redirect objects are the correct answers; print from them what you need to understand why you get into the redirect cycle; perhaps the server provides hints in the headers or body.

+9
source

Source: https://habr.com/ru/post/1484576/


All Articles