Parsing domain name in Python

1 reply [Last post]
ShadowMan
Offline
SX VIP
Joined: 2009/01/10

Someone in IRC mentioned parsing out a domain name using python, which should be pretty easy. I looked up some old py code from sometime ago and this should work, at least it did when I needed it to:

import re
text = 'https://Www.SomeDomain.co.uk'

print re.sub('http://', '', re.sub('https://', '', re.sub('www.', '', text.lower())))

Outputs:
somedomain.co.uk

You can embed additional re.subs to eliminate other modifiers such as forum. etc, but you only need to do the text.lower() once at the tail end.

Hope this helps