Assuming you have it in an accessible string, and assuming we want to be generic for having multiple levels on the top domain, you could:
token=my_string.split('http://')[1].split('/')[0]
top_level=token.split('.')[-2]+'.'+token.split('.')[-1]
We split first by the http://
to remove that from the string. Then we split by the /
to remove all directory or sub-directory parts of the string, and then the [-2]
means we take the second last token after a .
, and append it with the last token, to give us the top level domain.
There are probably more graceful and robust ways to do this, for example if your website is http://.com
it will break, but its a start :)