Extract domain name from url

I need to split the domain name from the entire URL field. I saved the url in a variable and from this variable I need to split only the domain name using Python. For example, http://www.google.com/ here I want to share only google from the entire URL.

+6
source share
1 answer

Try urlparse :

 >>> from urlparse import urlparse >>> urlparse('http://www.google.com/').hostname 'www.google.com' >>> urlparse('http://www.google.com/').hostname.split('.')[1] 'google' 

Also see helpful comments on how things can go if you have a complex domain name with subdomains - (just hostname.split('.')[1] will not work).

See also:

+6
source

Source: https://habr.com/ru/post/952094/


All Articles