0

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?

Mukul Kumar
  • 564
  • 1
  • 5
  • 24
  • 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? – Grismar Aug 03 '22 at 06:37
  • the best way is to splitting the string with `my_url_string[8:26]`. If you want a more dynamic way to extracts sub urls, that's another story – NicoCaldo Aug 03 '22 at 06:40
  • It's the snowflake login URL, so going to remain always same as above. That is `https://username.aws_region.snowflakecomputing.com`. So I want to get `username.aws_resgion` only. (The length of username can differ here) – Mukul Kumar Aug 03 '22 at 06:40

3 Answers3

1

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.

Grismar
  • 27,561
  • 4
  • 31
  • 54
1

You can use builtin library to extract hostname by using urllib.parse.

But you have to split string to extract subdomain after all.

from urllib.parse import urlparse

URL = "https://muk05119.us-east-1.snowflakecomputing.com"
parsed = urlparse(URL)

host = parsed.netloc  # => muk05119.us-east-1.snowflakecomputing.com
subdomain = '.'.join(host.split('.')[:2])
pu2x
  • 101
  • 5
0

You can use urlparse

from urllib.parse import urlparse
url = urlparse('https://muk05119.us-east-1.snowflakecomputing.com')
subdomain = url.hostname.split('.')[0] + '.' + url.hostname.split('.')[1]

where url.hostname.split('.')[x] where x indicates the subdomain. in your case, the first two subdomains need to be used, so 0 and 1

Documentation

NicoCaldo
  • 1,171
  • 13
  • 25