首页 > 解决方案 > 在 Python 中批量 WHOIS

问题描述

我正在尝试从 python 中的 txt 文件执行批量 WHOIS,我使用了以下代码,但没有得到输出 csv 文件。有人可以告诉我我做错了什么:

import whois
import csv

with open("domains.txt", "r") as f:
    domains = f.readlines()
    
with open("output_file.csv", "wb") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["Domain", "Registrant Name"])
    for domain in domains:
        domain = domain.rstrip()
        record = whois.whois(domain)
        try:
            r_name = record.registrant_name
        except AttributeError:
            r_name = "error"
        writer.writerow([domain, r_name])

我在 .py 文件中使用了代码并执行了它,没有任何反应。

然后我还尝试在 Python shell 中使用文件 C:\Users\path...\domains.txt 的完整路径运行(我尝试在路径中使用 1 和 2 个反斜杠)我得到了错误

SyntaxError: invalid syntax
>>>     writer = csv.writer(csvfile)
  File "<stdin>", line 1
    writer = csv.writer(csvfile)
    ^
IndentationError: unexpected indent 

标签: python

解决方案


推荐阅读