Python has a `urlparse` library that will let you do this in a more robust way.