python requests get redirect url

Python Requests Get Redirect URL

If you are working with web scraping or web automation, there might be times when you need to follow a redirect to access the desired page. Python Requests is a popular library for making HTTP requests in Python.

To get the final redirect URL using Python Requests library, you can use the allow_redirects parameter:


import requests

response = requests.get('https://www.example.com', allow_redirects=True)

final_url = response.url

print(final_url)

The above code will follow all redirects and return the final URL. The response.url attribute stores the final URL after all redirects have been followed.

You can also find out the list of all URLs that were redirected before the final URL. To do this, you can use the response.history attribute:


import requests

response = requests.get('https://www.example.com', allow_redirects=True)

redirect_history = response.history

print(redirect_history)

The above code will print a list of Response objects that represent each redirect. You can access the URL for each redirect using the url attribute of each Response object:


import requests

response = requests.get('https://www.example.com', allow_redirects=True)

redirect_history = response.history

for redirect_response in redirect_history:
    print(redirect_response.url)

The above code will print the URL for each redirect.

In addition to using the allow_redirects parameter, you can also use the max_redirects parameter to limit the number of redirects that Python Requests will follow:


import requests

response = requests.get('https://www.example.com', allow_redirects=True, max_redirects=5)

final_url = response.url

print(final_url)

The above code will follow a maximum of 5 redirects before returning the final URL.