代码工作良好,打印所有所需的值,但当我试图保存为df到csv所有的行是相同的。
a.csv文件内容
域
A.com
AMD.com
AMD.com
AOL.com
AOL.com
AWS.com
AWS.com
ammagazine.com3
dmail.com3
email.com3
xl.net3
净444.
4 email.com
4 email.net
4 mg.com
4 newyork.com
whdskcisail.com
wrestlingpages.com
wrexham.net
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('a.csv', nrows=100)
#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():
try:
DN = df.iloc[index]['Domains']
ip = socket.gethostbyname(DN)
ipwhois = IPWhois(ip).lookup_whois()
print(DN)
print(ip)
print(ipwhois)
df['IPcity'][index] = ipwhois['nets'][0]['city']
df['ASNumber'][index] = ipwhois['asn']
df['NetAddr'][index] = ipwhois['nets'][0]['address']
df['NetCity'][index] = ipwhois['city']
df['NetPostCode'][index] = ipwhois['nets'][0]['postal_code']
df.to_csv('a1.csv', index=False)
except Exception as e:
print(e)
df['e'] = e # want to save the errors in a differet column
df.to_csv('a2.csv', index=False)
发生这种情况是因为您在循环的每次迭代中都覆盖了.csv文件。如果将df.to_csv()
移出循环,您将得到所需的数据帧:
...
for index,row in df.iterrows():
try:
DN = df.iloc[index]['Domains']
ip = socket.gethostbyname(DN)
ipwhois = IPWhois(ip).lookup_whois()
print(DN)
print(ip)
print(ipwhois)
df['IPcity'][index] = ipwhois['nets'][0]['city']
df['ASNumber'][index] = ipwhois['asn']
df['NetAddr'][index] = ipwhois['nets'][0]['address']
df['NetCity'][index] = ipwhois['city']
df['NetPostCode'][index] = ipwhois['nets'][0]['postal_code']
except Exception as e:
print(e)
df['e'] = e # want to save the errors in a differet column
df.to_csv('a1.csv', index=False)