Extracting website names from link code cleanup I'm working on a program that takes website links and extracts the website name. Right now, it works perfectly, but it is very much a brute force approach and very ugly in my opinion.

Some of the properties of the links are as follows:
- entirely random
- will all contain https:// and .com
- may not contain www.

Here's a random link that I pulled up while shopping to give an example of what I'm talking about. Just to be 100% clear.

https://www.walmart.com/ip/Glencairn-Crystal-Whiskey-Glass-Set-of-2-Hailed-as-The-Official-Whiskey-Glass/183116827

remove 'https://www.'
remove '.com/....827'

result = walmart


And here is the code for it all:

f = open("Links.txt").read().split("\n")
f.remove('')

results = []

for i in f:
    if 'https://' in i:
        results.append(i[8:])

f = results
results = [f[0]]

for i in f:
    if 'www.' in i:
        results.append(i[4:])

f = results
results = []

for i in f:
    results.append(i[:i.find('.com')])

f = open("Results.txt", 'w')

for i in results:
    f.write(i + "\n")

f.close()


I tried cleaning it up with the re module, but it was throwing some errors when trying to search using re.search('www.(.*).com', f).

I was thinking about using the [a:] through [:b] notation in python, but since each link may not have 'www.' it seemed like it would be a faulty solution.

As I mentioned, this code works perfectly well. It's just such a simple thing that I want to get some practice in computer science and make it look like an elegant-ish solution.
As mentioned in the answer by Roland Illig, there is a module for that.

Example:

from urllib.parse import urlparse

def get_domain_name(url: str) -&gt; str:
    """Return domain name from URL
    """
    parsed_uri = urlparse(url)
    return parsed_uri.hostname


get_domain_name('https://www.ucl.ac.uk/alumni/services')


Result:


'www.ucl.ac.uk'


NB: this requires a full URL with http/https scheme.

Third-level domain extensions are handled with no problems. This is the prefered approach. The Python philosophy is that there should be one obvious way of accomplishing a given task. It doesn't have to be more complicated.

if you know all domain names will end in .com and want to get rid of it, just add .replace('.com', '').

A regular expression can also get the job done. Something like this will return walmart:

^https://(?:www\.)?([a-z0-9_\-]+)\.com


Assuming that the URL always starts with https://, and the domain name ends in .com, with www. optional and not included in the capture group. 

To explain this bit: (?:www\.)?
The rightmost question mark means the expression in parentheses is optional. ?: means: match the expression but do not capture it.

This is not an all-purpose expression, it is adjusted to your circumstances and should be good enough for your needs but that's it.

Let's say you have more complicated cases with subdomains, as an example:

url = 'www.subdomain.walmart.com'


Once you've got the full domain name, you could do some splitting then extract the relevant portions starting from the right, knowing that .com will be the last element:

bits=url.split('.')
print(bits)
['www', 'subdomain', 'walmart', 'com']
print(bits[-2])
walmart


I think this is slightly more flexible than working with fixed lengths like you're doing.

As for reading/writing files have a look at context managers (using the with statement).

Your code is not very long but you would still benefit by keeping the parsing in one dedicated function (possibly two), out of your loop. That would make the whole code easier to read and maintain.

One remark regarding www.: at some point you will find that some sites do redirect to ww1 or ww2 or stuff like that. Not to mention other types of subdomains. It is not safe to assume that the domain name will always be prefixed with www. or nothing.