Search code examples
pythonstringurltld

Python URL splitting


I have a string like google.com in Python, which I would like split into two parts: google and .com. The problem is where I have a URL such as subdomain.google.com, which I would like to be split into subdomain.google and .com.

How do I separate the rest of the URL from the TLD? It can't operate based on the last . in the URL because of TLDs such as .co.uk. Note the URL does not contain http:// or www.


Solution

  • tldextract looks like what you need. It deals with the .co.uk issue.