Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading