How to make requests in Python not follow redirects
If you're working with web scraping or APIs in Python using the requests library, you may come across the need to not follow redirects. This can be useful if you want to check if a certain URL is being redirected, or if you want to control the flow of your program and handle redirects yourself.
Method 1: Using the "allow_redirects" parameter
The simplest way to prevent requests from following redirects is to use the "allow_redirects" parameter in the request method. By default, this parameter is set to True, which means that requests will follow all redirects. To disable this behavior, simply set "allow_redirects" to False:
import requests
response = requests.get('https://example.com', allow_redirects=False)
This will send a request to "https://example.com" without following any redirects. If the server responds with a redirect status code (e.g. 301 or 302), the response object will contain the redirect URL in the "location" attribute.
Method 2: Using the "hooks" parameter
If you want more control over how redirects are handled, you can use the "hooks" parameter to register a callback function that will be called each time a redirect is encountered. This function can inspect the response and decide whether to follow the redirect or not.
import requests
def handle_redirect(response, **kwargs):
if response.is_redirect:
# do something with the redirect URL
return False
return response
response = requests.get('https://example.com', hooks={'response': handle_redirect})
In this example, we define a callback function called "handle_redirect" that takes a response object and a variable number of arguments (the **kwargs syntax allows us to capture any additional arguments that may be passed). This function checks if the response is a redirect (using the "is_redirect" attribute) and decides whether to follow it or not. If the function returns False, the redirect will not be followed.
We then pass this function to the "hooks" parameter of the request method, with the key "response" to indicate that it should be called for each response object.
Method 3: Using a custom session object
If you want to use the same redirect handling behavior across multiple requests, you can create a custom session object that has the desired behavior. This can be useful if you want to set default headers or cookies for all requests, or if you want to use a persistent connection to the server.
import requests
session = requests.Session()
session.max_redirects = 0
response = session.get('https://example.com')
In this example, we create a new Session object and set its "max_redirects" attribute to 0, which disables all redirects. We can then use this session object to send requests, and they will all follow this redirect handling behavior.
Note that this method will also persist cookies and auth tokens across requests, so it may not be appropriate for all use cases.