【问题标题】:Doing a whois with a list of domain names使用域名列表进行 whois
【发布时间】:2015-01-03 03:32:39
【问题描述】:

我有一个域名文件,例如相当于 2500。

我想对这些域名进行whois。

问题是我从来没有这样做过,也不知道从哪里开始。如果您有任何想法,我会全力以赴。

TIA。

【问题讨论】:

  • 您是否只想下载列表中每个域的每个 whois 记录?或者您想解析出注册人信息?
  • 嗨,Ajcr,解析出注册人信息是什么意思?
  • 嗨@AndyK,我只是想知道您希望捕获什么:如果您希望简单地获取每个域的 whois 查找结果(例如,将每个查找的结果写入其自己的文本文件)或者如果您希望自动提取域所有者的联系信息(例如姓名、电话号码)并将这些信息存储在例如 CSV 文件中。
  • 嗨@Ajcr,我会说第二个选项。但我不知道该怎么做......
  • 请举例说明该文件中的单个记录,以及您希望从该记录中得到什么答案。

标签: web whois pywhois


【解决方案1】:

假设域在一个名为 domains.txt 的文件中,并且您安装了 pywhois,那么这样的事情应该可以解决问题:

import whois

infile = "domains.txt"

# get domains from file
with open(infile, 'rb') as f:
    domains = [line.rstrip() for line in f if line.rstrip()]

for domain in domains:
    print domain
    record = whois.whois(domain)

    # write each whois record to a file {domain}.txt
    with open("%s.txt" % domain, 'wb') as f:
        f.write(record.text)

这会将每个 whois 记录输出到一个名为 {domain}.txt 的文件中


没有pywhois

import subprocess

infile = "domains.txt"

# get domains from file
with open(infile, 'rb') as f:
    domains = [line.rstrip() for line in f if line.rstrip()]

for domain in domains:
    print domain
    record = subprocess.check_output(["whois", domain])

    # write each whois record to a file {domain}.txt
    with open("%s.txt" % domain, 'wb') as f:
        f.write(record)

【讨论】:

  • 嗨,马丁,在我收到错误 record = whois.whois(domain) AttributeError: 'module' object has no attribute 'whois' 之前几乎工作得很好。我试图用pywhois 更改它,但我得到了那个错误而不是record = whois.whois(domain) NameError: name 'whois' is not defined
  • @AndyK 你安装了 pywhois 吗?你可以这样安装:pip install python-whois
  • Bonjour Céline,我已经安装了pywhois。使用pywhois 时,我收到此错误record = whois.whois(domain) NameError: name 'whois' is not defined。悲惨……
  • hmm 你可能和这个人有同样的问题:code.google.com/p/pywhois/issues/detail?id=53 可能安装了两个 pywhois? (请注意,如果您使用easy_install pywhois,您将获得不同于 python 库的 CLI 工具)
  • 如果您在使用 whois 软件包时遇到问题,那么您可以使用 record = subprocess.check_output(["whois", domain]) 而不是 record = whois.whois(domain)。还将import whois 更改为import subprocess。这应该适用于 Unix,但我不确定 Windows。
【解决方案2】:

您还可以使用 Linux 命令工具whois。 以下代码打开一个子进程并搜索域。

但是你必须小心在短时间内处理许多请求。一段时间后,服务器最终会阻止您。 ;)

import subprocess

def find_whois(domain):
    # Linux 'whois' command wrapper
    # 
    # Executes a whois lookup with the linux command whois.
    # Returncodes from: https://github.com/rfc1036/whois/blob/master/whois.c

    domain = domain.lower().strip()
    d = domain.split('.')
    if d[0] == 'www': d = d[1:]

    # Run command with timeout
    proc = subprocess.Popen(['whois', domain], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    ans,err = proc.communicate(input)

    if err == 1: raise WhoisError('No Whois Server for this TLD or wrong query syntax') 
    elif err == 2: raise WhoisError('Whois has timed out after ' + str(whois_timeout) + ' seconds. (try again later or try higher timeout)')
    ans = ans.decode('UTF-8')
    return ans


with open('domains.txt') as input:
    with open('out.txt','a') as output:
        for line in input:
            output.write(find_whois(line))

with open as 语句处理文件流。 输出文件中的“a”表示文件以附加模式打开。

【讨论】:

  • 嗨,Alu,谢谢,但是... :) 我有一个文件 domains.txt,其中将包含像 www.coca-cola.frwww.apple.com 这样的域名。请问有没有办法处理文件并将其保存到另一个文件?
  • 嗨,Alu,我收到了那个错误Traceback (most recent call last): File "<stdin>", line 4, in <module> File "<stdin>", line 5, in find_whois File "C:\Program Files\python27\lib\subprocess.py", line 710, in __init__ errread, errwrite) File "C:\Program Files\python27\lib\subprocess.py", line 958, in _execute_chil d startupinfo)
  • 我想我遇到了问题。 Windows 上没有谁... -_- 需要仔细检查,但这听起来可能是一个解释。我会通知你的。
  • 是的,在 Windows 中没有这样的命令行工具。也许您从 Windows-Sysinternals 工具包中寻找 whois 工具。 technet.microsoft.com/de-de/sysinternals/bb897435.aspx
  • 嗨,Alu,您的解决方案运行良好。虽然,服务器似乎越来越少允许在其服务器上使用whois。不管结果出乎意料的糟糕,因为您是第一个向我提供有关您的解决方案的详细信息的人,所以 50 分是您的。
【解决方案3】:

您似乎已经得到了一些有用的答案,但我认为最好多谈谈批量(以及一般)进行 WHOIS 查询所面临的挑战并提供一些替代解决方案。

WHOIS 查询

查找单个域名通常需要找到该域的相关 WHOIS 服务器,然后通过端口 43 请求信息。如果您可以访问类似 unix 的 shell(例如 Bash),则可以使用 whois 来轻松做到这一点(正如其他人所指出的那样):

$ whois example.com

非常相似的 WHOIS 工具也可用作大量编程语言的模块。 Python 的 pywhois 模块就是一个例子。

在最简单的形式中,批量 WHOIS 查询只是遍历域列表,为每个域发出 whois 请求并将记录写入输出。

这是 Bash 中的一个示例,它从文件 domains.txt 读取域并将每个 WHOIS 记录写入单独的文件(如果您使用的是 Windows,请尝试 Cygwin)。

#!/bin/bash

domain_list="domains.txt"

while read line 
do
    name=$line
    echo "Looking up ${line}..."
    whois $name > ${line}.txt
    sleep 1
done < $domain_list

注意批量 WHOIS 查询的以下并发症:

  • 某些 WHOIS 服务器可能无法为您提供完整的 WHOIS 记录。对于特定国家/地区的域(例如 .de 和 .fr)和在某些注册商(例如 GoDaddy)注册的域尤其如此。

    如果您想要尽可能完整的记录,您通常必须访问注册表的网站或可能已缓存记录的第三方服务(例如 DomainTools)。这更难以自动化,可能必须手动完成。即使这样,记录也可能不包含您想要的内容(例如注册人的联系方式)。

  • 某些 WHOIS 服务器会限制您在特定时间范围内可以发出的请求数量。如果您达到了限制,您可能会发现您必须在几个小时后返回才能再次请求记录。例如,对于 .org 域,您在一分钟内最多只能进行 3 次查找,并且一些注册商会在 24 小时内禁止您。

    最好在查找之间暂停几秒钟,或尝试按 TLD 重新排列您的域列表,这样您就不会快速连续多次打扰同一台服务器。

  • 一些 WHOIS 服务器经常停机,请求会超时,这意味着您可能需要返回并重新进行这些查找。 ICANN 规定 whois servers must have a pretty decent uptime,但我发现一两台服务器在提供记录方面很糟糕。

解析记录

解析 WHOIS 记录(例如,用于注册人联系信息)可能是一项挑战,因为:

  • 记录的格式并不总是一致的。您会在 .com 域中发现这一点。 .com 记录可能由全球数以千计的注册商中的任何一个持有(不是由 .com 注册机构,威瑞信),并且并非所有人都选择以 ICANN 推荐的易于解析的格式呈现记录。

  • 同样,您要提取的信息可能不在您从查找返回的记录中。

既然已经提到过,pywhois 是解析 WHOIS 数据的一种选择。这是一个非常简单的 Python 脚本,它查找每个域的 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])

* 当我快速测试这个脚本时,pywhois 在提取注册人姓名时不是很可靠。您可以尝试的另一个类似库是pythonwhois

【讨论】:

  • ajcr,非常感谢您的回答。我曾尝试在google.comoracle.com 上进行whois,但他们不会让我这样做。该死。谢谢你的一切。
  • @AndyK - 完全没问题。一些 .com 和 .net 域(如 google.com)可能很难查询,因为 Versign WHOIS 服务器会返回包含域名的域 服务器的详细信息。一些解析器不知道如何处理这个并感到困惑(这很可能就是这种情况)。如果还有什么我可以贡献的,请告诉我。
  • 不,ajcr。我应该很好。我需要看看我的方法是否可以解决问题,但事实并非如此,我有一个 B 计划。所以我们很好。非常感谢。
【解决方案4】:

http://technet.microsoft.com/en-us/sysinternals/bb897435.aspx下载并安装微软的whois工具

创建一个包含域名列表的文本文件,并带有标题行。

name
google.com
yahoo.com
stackoverflow.com

创建一个 powershell 脚本:

$domainname = Import-Csv -Path "C:\domains.txt"
foreach($domain in $domainname) 
{
   .\whois.exe $domain.name Export-Csv -Path "C:\domain-info.csv" -Append
}

运行 powershell 脚本。

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多