-1

I made a script that opens the csv file which contains multiple domain names and then uses python-whois module to find the emails for each domain.

import whois
import csv


with open('emails.csv', 'r', newline='') as f:
    reader = csv.reader(f)
    distro = [row for row in reader]

with open('emails_done.csv', 'w', newline='') as f:
    wr = csv.writer(f)
    for s in distro:
        try:
            if whois.whois(s[0]).emails is not None:
                if len(whois.whois(s[0]).emails[0]) == 1:
                    wr.writerow([s[0]] + [whois.whois(s[0]).emails])
                else:
                    wr.writerow([s[0]] + whois.whois(s[0]).emails)
            elif whois.whois(s[0]).registrant_email is not None:
                print(whois.whois(s[0]).registrant_email[1])
                wr.writerow([s[0]] + [whois.whois(s[0]).registrant_email])
            else:
                wr.writerow([s[0]] + ['nothing found'])
        except Exception as e:
            print(e)
            wr.writerow([s[0]] + ['invaliddomain'])

The problem is that this module gives me different data for the same domain every time I run a code. For example, the first time I used ".emails" method on the domain it returned a list of 2 emails, but when I re-run the same code for the second time it returned me a string with 1 email instead of a list. ".emails" method I used in the code returns a list of all emails it found from the whois data if there's more than 1 email available, but it returns a string instead of list if there's only 1 email available, so I need to convert this string into the list so I can put it in csv.writerow method. Is there a mistake from my side or it's just the way this module works and there's nothing I can do about it?

Xanya
  • 13
  • 3

1 Answers1

0

This line here maybe your problem:

if len(whois.whois(s[0]).emails[0]) == 1:

I assume you're checking if only one email is available so you instead need to use:

if len(whois.whois(s[0]).emails) == 1:
M H
  • 154
  • 3