import requests
from bs4 import BeautifulSoup

def get_page_title(url):
    """
    Fetches the content of a given URL and extracts the title of the HTML page.

    Args:
        url (str): The URL of the webpage to fetch.

    Returns:
        str: The title of the webpage, or a message indicating an error.
    """
    try:
        # Send an HTTP GET request to the URL
        response = requests.get(url)
        response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

        # Parse the HTML content using BeautifulSoup
        soup = BeautifulSoup(response.text, 'html.parser')

        # Find the <title> tag and extract its text
        title_tag = soup.find('title')
        if title_tag:
            return title_tag.get_text()
        else:
            return "Title tag not found on the page."

    except requests.exceptions.RequestException as e:
        return f"Error fetching URL: {e}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"

if __name__ == "__main__":
    target_url = "https://www.example.com"  # Replace with a URL you want to test
    page_title = get_page_title(target_url)
    print(f"The title of '{target_url}' is: {page_title}")

    # Example with a non-existent URL to demonstrate error handling
    invalid_url = "https://this-is-not-a-real-website.com"
    invalid_page_title = get_page_title(invalid_url)
    print(f"\nAttempting to fetch '{invalid_url}': {invalid_page_title}")
