pythonstringurlurlparse

split URL python


I have a URL https://muk05119.us-east-1.snowflakecomputing.com and I want to retrieve only muk05119.us-east-1 from this.

Instead of splitting the string and retrieving the above, what is the best way to accomplish this?


Solution

  • Your example is clear by itself, but it's unclear what rule underlies it. Do you want the first two parts of the domain? All but the last two parts of the domain? Do you want everything before a main domain name and the top level domain (e.g. before .google.com but also before .australia.gov.au)? Or some other rule still?

    The first two parts:

    from urllib.parse import urlparse
    
    url = 'https://muk05119.us-east-1.snowflakecomputing.com'
    netloc = urlparse(url).netloc
    
    print(netloc[:netloc.index('.', netloc.index('.')+1)])
    

    Or:

    print('.'.join(netloc.split('.')[:2]))
    

    All but the last two parts:

    print('.'.join(netloc.split('.')[:-2]))
    

    For everything before the main and top-level domain, have a look at https://pypi.org/project/publicsuffixlist/ and use that with some of the above.