Python get redirect url

How to get redirect url using python requests?

When making HTTP requests with the Python library ‘requests’, it is sometimes necessary to handle redirects. A redirect is a way for a server to direct a client to a different URL than the one that was originally requested. This can happen for a variety of reasons, such as a page being moved or a user being redirected to a login page. In order to handle redirects properly, it is important to understand how to get the redirect URL using the ‘requests’ library.

Method 1: Using the ‘allow_redirects’ parameter

To get the redirect URL using Python requests with the ‘allow_redirects’ parameter, you can follow these steps:

response = requests.get(url, allow_redirects=True)
if response.history: print("Request was redirected")

Here’s the complete example code:

import requests url = 'http://example.com' response = requests.get(url, allow_redirects=True) if response.history: print("Request was redirected") for resp in response.history: print(resp.status_code, resp.url) print("Final destination:") print(response.status_code, response.url) else: print("Request was not redirected") final_url = response.url print("Final URL:", final_url)

This code will send a GET request to the specified URL with the ‘allow_redirects’ parameter set to True. If the response is a redirect, it will print out the status code and URL for each redirect, as well as the final destination URL. Finally, it will print out the final URL after all redirects.

Читайте также:  window on js

Note that the ‘allow_redirects’ parameter is set to True by default in requests.get() method, so you can omit it if you want.

Method 2: Using the ‘history’ attribute

To get the redirect URL using Python requests library, you can use the ‘history’ attribute. Here are the steps to do it:

response = requests.get('http://example.com')
if response.history: print("Request was redirected") for resp in response.history: print(resp.status_code, resp.url) print("Final destination:") print(response.status_code, response.url)

In the above code, we check if the response has a redirect using the ‘history’ attribute. If it has a redirect, we print the status code and URL of each redirect using a loop. Finally, we print the status code and URL of the final destination.

Here is the complete code:

import requests response = requests.get('http://example.com') if response.history: print("Request was redirected") for resp in response.history: print(resp.status_code, resp.url) print("Final destination:") print(response.status_code, response.url) else: print("Request was not redirected")

This code will print the redirect URL and status codes in the console.

Method 3: Using the ‘next’ attribute

To get the redirect URL using Python requests library, you can use the ‘next’ attribute. Here’s how you can do it in a few simple steps:

if response.status_code == 302:
redirect_url = response.next.url
import requests url = 'https://example.com' response = requests.get(url) if response.status_code == 302: redirect_url = response.next.url print(redirect_url)

This code sends a GET request to the specified URL and checks if the response has a redirect status code. If it does, the code gets the redirect URL using the ‘next’ attribute and prints it to the console.

Note that the ‘next’ attribute may not always be present in the response, depending on the server configuration. In such cases, you can try using the ‘location’ attribute instead.

Источник

Оцените статью