【问题标题】:Bulk Whois lookup Of 20,000 domains - getting timeouts批量查找 20,000 个域的 Whois - 获取超时
【发布时间】:2023-04-07 22:07:01
【问题描述】:

我正在尝试批量提取 20,000 个域名的 WHOIS 信息,python 代码适用于我的 csv 文件中的 2 个项目,但会导致 20000 个域名的整个数据集出错

试过2个域名,OK。使用 20k 域名的完整列表会导致错误

import whois
import matplotlib.pyplot as plt
import numpy as np  
import pandas as pd  
import socket
import os
import csv 
import datetime
import time
import requests
from ipwhois import IPWhois
from urllib import request
from ipwhois.utils import get_countries
import tldextract
from ipwhois.utils import get_countries
countries = get_countries(is_legacy_xml=True)
from ipwhois.experimental import bulk_lookup_rdap
from ipwhois.hr import (HR_ASN, HR_ASN_ORIGIN, HR_RDAP_COMMON, HR_RDAP, HR_WHOIS, HR_WHOIS_NIR)
countries = get_countries(is_legacy_xml=True)
import ipaddress

df = pd.read_csv('labelled_dataset.csv')

#TimeOut Setting
s = socket.socket()
s.settimeout(10)

#Date Processing Function

def check_date_type(d):
    if type(d) is datetime.datetime:
        return d
    if type(d) is list:
        return d[0]
for index,row in df.iterrows():

    DN = df.iloc[index]['Domains']

    df['IPaddr'] = socket.gethostbyname(DN)
    df['IPcity'] = IPWhois(socket.gethostbyname(DN),     allow_permutations=True).lookup_whois()['nets'][0]['city']
    df['ASNumber'] = IPWhois(socket.gethostbyname(DN), allow_permutations=True).lookup_whois()['asn']
    df['NetAddr'] = IPWhois(socket.gethostbyname(DN), allow_permutations=True).lookup_whois()['nets'][0]['address']
    df['NetCity'] = IPWhois(socket.gethostbyname(DN), allow_permutations=True).lookup_whois()['nets'][0]['city']
    df['NetPostCode'] = IPWhois(socket.gethostbyname(DN), allow_permutations=True).lookup_whois()['nets'][0]['postal_code']
    W = whois.whois(DN)
    df['WebsiteName'] = W.name
    df['ASRegistrar'] = W.registrar
    df['CtryCode'] = W.country
    df['Dstatus'] = W.status[1]
    df['RegDate'] = check_date_type(W.creation_date)
    df['ExDate'] = check_date_type(W.expiration_date)

df.to_csv('extracted_dataset_1_1.csv', index=False)

期望输出 ASN 详细信息,每个域名的 WHOIS 信息导出到 csv 文件中

【问题讨论】:

    标签: python arrays bulkupdate pywhois


    【解决方案1】:

    您正在为要查找的每个属性创建一个新的IPWhois 对象。这意味着每次迭代至少运行 5 个whois 查询。

    这会产生大量网络流量,而且完全没有必要 - 您可以在每个域中运行一次 whois 并以成员身份访问结果。

    尝试将循环中的代码更改为以下内容:

    df['IPaddr'] = socket.gethostbyname(DN)
    ipwhois = IPWhois(df['IPaddr'], allow_permutations=True).lookup_whois()
    if (ipwhois):
      df['IPcity'] = ipwhois['nets'][0]['city']
      df['ASNumber'] = ipwhois['asn']
      df['NetAddr'] = ipwhois['nets'][0]['address']
      df['NetCity'] = ipwhois['city']
      df['NetPostCode'] = ipwhois['nets'][0]['postal_code']
    

    还有一些我建议的其他优化:

    • 在每次迭代或每 n 次迭代时写入您的文件,以便您可以增量工作,并且在代码错误时不会丢失结果。
    • 使用一个库 - IPWhoiswhois - 不能同时使用。
    • 看看使用aysyncio。目前,您的代码必须等待来自whois 查询的响应才能继续,并且网络查询比您的代码在循环中的每次迭代中运行要慢许多数量级。使用异步模型,您可以触发多个 whois 查询,并且仅在结果到达时对其进行操作。此模型有助于优化您的应用程序的效率。

    【讨论】:

    • 谢谢,aysyncio 听起来不错。我现在如何将它插入到这个特定的代码中。顺便说一句,我尝试按照建议更改代码,但最终只为索引中的所有项目提取了 1 个 IP 地址。和一个错误:raise ValueError('%r 似乎不是 IPv4 或 IPv6 地址'%
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多