How to remove duplicate domains from text files using python?

ScrapeboxWorker

Regular Member
Joined
Jul 23, 2012
Messages
498
Reaction score
285
Usage in command line:
Code:
python remove_duplicate_domains.py input.txt output.txt --toplevel=true
Code:
import tldextract
import argparse


def rm_dup_domains(args):
    file_in = open(args.input, 'r', encoding='utf-8', errors='ignore')
    file_out = open(args.output, 'a', encoding='utf-8', errors='ignore')
    table = dict()

    for line in file_in:
        url = tldextract.extract(line.strip())

        if args.toplevel is True:
            domain = url.registered_domain

        elif args.toplevel is False:
            if url.subdomain == 'www' or url.subdomain == '':
                domain = url.registered_domain

            else:
                domain = url.fqdn
                if domain.startswith('www.'):
                    domain = url.fqdn.replace('www.', '', 1)

        if domain != '':
            # lookup for duplicates
            if table.get(domain) is not None:
                pass
            else:
                table[domain] = True
                file_out.write(f'{domain}\n')


if __name__ == '__main__':
    # create command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('input', help='input file', type=str)
    parser.add_argument('output', help='output file', type=str)
    parser.add_argument('--toplevel', help='keeps only unique top-level domains (removes sub-domains)', type=bool, default=False)
    parser.parse_args()
    # parse them
    args = parser.parse_args()
    # run code
    rm_dup_domains(args)
Your post in the thread How to remove duplicate domains from text files using python? was deleted. Reason: Thread is from 2015, no need to bump it now
3 minutes ago

Yes very clever moderator... i won't use real epithets that i think what are you...
Now beacuse you deleted my reply, you have:
1. A duplicate thread... great!
2. Old topic was indexed in google and could drive traffic to this website... people looking for "how to remove duplicate domains in python" were coming to this forum and registering... now you have a fresh one that will take ages to index like the old one... great!
3. Instead of keep things in one topic, we will have 22340980 different threads...

Great work!
 
Yes very clever moderator... i won't use real epithets that i think what are you...
Now beacuse you deleted my reply, you have:
1. A duplicate thread... great!
2. Old topic was indexed in google and could drive traffic to this website... people looking for "how to remove duplicate domains in python" were coming to this forum and registering... now you have a fresh one that will take ages to index like the old one... great!
3. Instead of keep things in one topic, we will have 22340980 different threads...

Actually, that was exactly the intention the moderator had behind warning you for necro-bumping - we'd rather have fresh threads created on topics instead of old, out-of-date ones being bumped instead of this.

If you'd have posted this without the passive-aggressive messages about having your post deleted, it would have been absolutely fine.
 
Actually, that was exactly the intention the moderator had behind warning you for necro-bumping - we'd rather have fresh threads created on topics instead of old, out-of-date ones being bumped instead of this.

If you'd have posted this without the passive-aggressive messages about having your post deleted, it would have been absolutely fine.

Why i can't see who deleted my post? In the alert i received there was no nick of the moderator ? Anonymous aha ?

uF6WGMY


Second in google... nice source of traffic... nice place to put usefull info for people to be found... if you think that creating a new thread is good for the forum and members itself then tell me why?
Tell me what is logic behind this?
 
On linux all you gotta do is:

Code:
cat FILE.txt | sort | uniq
 
Why i can't see who deleted my post? In the alert i received there was no nick of the moderator ? Anonymous aha ?

uF6WGMY


Second in google... nice source of traffic... nice place to put usefull info for people to be found... if you think that creating a new thread is good for the forum and members itself then tell me why?
Tell me what is logic behind this?

It's just a part of the rules. If you have an issue with this rule, you should post a thread in the Forum Suggestions and Feedback section or contact our support email.
 
You hadn't specified they were URLs and not domains! In that case it's:

Code:
cat FILE.txt | perl -pne 's/http[s]*\:\/\/([^\/]+)[\/]*.*/$1/' | sort | uniq

Works for both domains and urls.

Ah yes i should specify that whatever urls you input in the script, you will receive clean:
domain.com
domain2.com
in the output
 
Glad you could get your signature in this thread... still would be better to have in the old thread i guess.

Did you click on the sig? Hope you did.

Anyway, you have been a member here for several years so should know that bumping threads older than 6 months is against forum etiquette and doesn't help people.
The fact that nobody responded to the thread since 2015 should have told you that it was not necessary and unimportant.
Besides, there are numerous/easier ways of removing duplicates from a text list.
 
Back
Top