What is the purpose of the 'allow_redirects' parameter in Requests?

In Python's requests library, the allow_redirects parameter is used to control whether or not the library should automatically follow HTTP redirects (status codes like 301, 302, 303, 307, and 308). By default, allow_redirects is set to True, meaning that if the server responds with a redirect, the requests library will automatically perform the redirection to the new URL provided in the response headers.

When you set allow_redirects to False, the library will not follow redirects and will instead return the original response that includes the redirect status and the new location to which it is attempting to redirect.

Here is an example of how to use allow_redirects in the requests library:

import requests

# By default, allow_redirects is True, and this request will follow any redirects.
response_with_redirects = requests.get('http://example.com/some-redirect-url')
print("URL after redirect:", response_with_redirects.url)

# You can disable redirection by setting allow_redirects to False.
response_without_redirects = requests.get('http://example.com/some-redirect-url', allow_redirects=False)
print("Status code when redirects are not followed:", response_without_redirects.status_code)
print("URL without following redirect:", response_without_redirects.url)

if response_without_redirects.is_redirect:
    # You can manually handle the redirect if needed.
    new_location = response_without_redirects.headers['Location']
    print("Redirect location from headers:", new_location)

In the example above, if 'http://example.com/some-redirect-url' responds with a redirect, the first request will follow it and return the final response after all redirects have been followed. The second request, however, will return the initial redirect response, allowing you to inspect the status code or headers and manually handle the redirection if desired.

Using allow_redirects=False can be useful when you need to: - Inspect redirect responses without following them. - Capture cookies that are set before a redirect. - Control the redirection process manually (e.g., to handle specific redirect logic or avoid redirect loops). - Implement custom logic based on the presence of redirects.

Remember that allow_redirects works with both GET and HEAD requests, but it does not apply to methods that are not supposed to follow redirects, such as POST, PUT, DELETE, etc.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon