Extracting website names from link code cleanup
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.