There are a lot of great tools out there to extract urls/domais/ips/etc from text, but one of the most useful that I've found was tldextract.

You can easily install it with pip (pip install tldextract), the project page has some documentation and some examples.

A small code snippet that opens a text file and extracts the domain part ( is transormed into for example )

import tldextract
with open("input.txt") as f:
    for line in f:
        extracted = tldextract.extract(line)
        print "{}.{}".format(extracted.domain, extracted.suffix)