All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Get Domain From URL

Last Updated : Mar 11, 2024

Python Get Domain From URL

In this article we will show you the solution of python get domain from url, an alternative term for a web address is a URL. For instance, Javatpoint.com is a word-based URL. Additionally, IP addresses can be utilized to create URLs (for instance, 192.168.2.24).

Most users provide the name's address when conducting an internet search because names are easier to remember than numbers.

URLs are used by web browsers to request specific pages from online servers.

For numerous reasons, such as extracting the domain name for web analytics or security, getting the domain name from a URL in javascript might be useful.

We will now discuss the idea of how to get domain from url in python with an example.

Step By Step Guide On Python Get Domain From URL :-

import re
def getDomain(url:str) -> str:
    '''
        Return the domain from any url
    '''
    # copy the original url text
    clean_url = url
    # take out protocol
    reg = re.findall(':[0-9]+',url)
    if len(reg) > 0:
        url = url.replace(reg[0],'')
    # take out paths routes
    if '/' in url:
        url = url.split('/')
    # select only the domain
    if 'http' in clean_url:
        url = url[2]
    # preparing for next operation
    url = ''.join(url)
    # select only domain
    url = '.'.join(url.split('.')[-2:])
    return url
  1. The domain from the given URL is extracted by the method getDomain, which accepts a string url as input. The function's goal is to remove the domain from any URL.
  2. A duplicate of the original URL is kept in the variable clean_url.
  3. To locate a substring in an url that matches a colon and one or more digits, use the regular expression:[0-9]+. This will locate and, if necessary, eliminate the port number. Use re.findall() to find every instance of this pattern in the URL.
  4. The URL contains a port number if the re.findall() function finds any matches. The first matching substring is then replaced with an empty string using the replace() function. This removes the port number from the URL.
  5. The URL must then be cleared of any paths and routes. A forward slash ('/') in the URL implies that there are paths or routes available. The split() function splits the URL into a list of substrings using the '/' delimiter. The domain is separated from the rest of the URL in this way.
  6. The protocol is included if the original URL begins with "http". In this instance, the domain is represented by the split URL's third element (index 2). This is kept in the url variable.
  7. The join() function is used to rejoin the split URL and create a string.
  8. By employing '.' as a delimiter to divide the string into a list of substrings, the domain is extracted from it. The domain is represented by the last two components of this list (indices -2 and -1). To create the final domain string, these pieces are combined using the join() method with a '.' as the delimiter.
  9. The result of the function is the last domain string.

Conclusion :-

As a result, we have successfully learned how to get domain from url in python with an example.

In javascript, there are several techniques to retrieve the domain name from a URL.

Using the window.location object is the fastest and safest approach to get the domain name from the current URL.

I hope this article on python get domain from url helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪