Home >Backend Development >PHP Tutorial >How Can I Easily Extract the Domain Name from a URL in Python?

How Can I Easily Extract the Domain Name from a URL in Python?

Susan Sarandon
Susan SarandonOriginal
2024-12-14 02:32:091012browse

How Can I Easily Extract the Domain Name from a URL in Python?

Extracting the Domain from a URL

Extracting the domain from a URL is a common task in web development. For instance, you might need to identify the host of a request for logging or security purposes. Here's a simple function that can handle this task:

def get_domain(url):
    parsed = urlparse(url)
    return parsed.netloc

This function uses Python's built-in urlparse function to parse the URL into its components, including the domain name. The netloc attribute of the returned object contains the host and port of the URL.

Here's how it works:

url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html'
domain = get_domain(url)
print(domain)  # Prints: google.com

The get_domain() function also works with other URL schemes, such as HTTPS and FTP.

url = 'https://www.google.com/dhasjkdas/sadsdds/sdda/sdads.html'
domain = get_domain(url)
print(domain)  # Prints: www.google.com

Since the urlparse function is a standard library function available in Python, you can use this technique in different projects and platforms without the need for third-party libraries.

The above is the detailed content of How Can I Easily Extract the Domain Name from a URL in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn