Selected Reading

Python response.url Attribute



The response.url attribute of the Python Requests module which provides the final URL of the response after any redirection.

When we make an HTTP request the server might redirect us to a different URL. The response.url attribute captures the final destination URL by reflecting any changes from the original request.

This attribute is particularly useful for tracking where our request ended up especially when dealing with redirects, shortened URLs or authentication flows. Accessing response.url is straightforward which simply use response.url after making a request.

Syntax

Following is the syntax and parameters of response.url attribute of the Python Requests module −

response.url

Parameter

This attribute does not accept any parameters.

Return value

This attribute returns the final url.

Example 1

Following is the example of response.url attribute of the Python Requests module which allows us to handle HTTP requests easily and provides access to information about redirects −

import requests

def track_redirects(url):
    response = requests.get(url, allow_redirects=True)
    history = response.history
    
    print("Tracking redirects for URL:", url)
    if history:
        print("Redirect chain:")
        for resp in history:
            print(resp.status_code, resp.url)
        print("Final destination:")
        print(response.status_code, response.url)
    else:
        print("No redirects. Final URL:", response.url)

# Example usage
track_redirects('http://www.tutorialspoint.com')  # Replace with the URL you want to track

Output

Tracking redirects for URL: http://www.tutorialspoint.com
No redirects. Final URL: http://www.tutorialspoint.com/

Example 2

In this example we checks if both the scheme i.e. http, https and the network location i.e. www.tutorialspoint.com are present by indicating a well-formed URL. −

from urllib.parse import urlparse

def is_valid_url(url):
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc])
    except ValueError:
        return False

# Example usage
url = "https://www.tutorialspoint.com/path/to/resource"
if is_valid_url(url):
    print("URL is valid:", url)
else:
    print("Invalid URL:", url)

Output

URL is valid: https://www.tutorialspoint.com/path/to/resource

Example 3

A URL fragment is the part of a URL that starts with a '#' symbol and is used to specify a specific section or anchor within a resource typically in HTML documents. Following is the example of it −

import requests

def get_final_url_with_fragment(url):
    try:
        response = requests.get(url, allow_redirects=True)
        final_url = response.url
        print("Final URL:", final_url)
        
        # Check if there's a fragment in the final URL
        if '#' in final_url:
            fragment = final_url.split('#', 1)[1]
            print("Fragment:", fragment)
    except requests.exceptions.RequestException as e:
        print("Error:", e)

# Example usage
url_to_check = "http://example.com#section"
get_final_url_with_fragment(url_to_check)

Output

Final URL: http://example.com/#section
Fragment: section
python_modules.htm
Advertisements