0
我工作的一個python3腳本,執行以下操作:「‘NoneType’對象不是標化」或「KeyError異常:」與openpyxl和ipwhois
- 打開一個Excel文件中的工作目錄
- 選擇在Excel中第一片材文件
- 通過所有的IP地址的選擇在第三列(一範圍的IP地址在此情況下)的所有數據
- 迭代並調用爲每個
- 商店的whois API每個IP的結果變量(以.json)
- 通過解析結果找名稱,IP範圍,聯繫方式
- 上面寫到Excel新行從6文件中的值
- 保存Excel用在一個新的文件的文件名當前目錄
當前文檔具有427個唯一IP地址的列表,並且來自whois api的RIPE名稱的結果是唯一的(有時在相同的響應中)。爲了適應這種情況,我已經遍歷每個RIPE名稱以獲取['contact']列表中的次級數據集。只要聯繫人列表包含我想要的值,此工作正常。如果沒有,我會得到一個NoneType的錯誤。我試圖用if語句來構建預防性邏輯,其中result == None爲我的變量賦予一個'NULL'值,但隨後我在RIPE名稱上得到了一個KeyError異常。我卡住了,需要你的幫助。這裏是我的代碼的副本:
import openpyxl
from pprint import pprint
from ipwhois import IPWhois
wb = openpyxl.load_workbook('Abuse Log with Notes FWC 2016-06-09.xlsx')#change name here to file to be used
sheet = wb.get_sheet_by_name('Sheet1') #get first sheet in workbook
#Add new column headings for API results
sheet['E1'] = 'HOST NAME'
sheet['F1'] = 'HOST COUNTRY'
sheet['G1'] = 'IP START'
sheet['H1'] = 'IP END'
sheet['I1'] = 'HOST EMAIL'
sheet['J1'] = 'HOST PHONE'
sheet['K1'] = 'HOST ADDRESS'
#Store all start range IP's for Amazon in one list variable
AmazonStartIPs = [
'54.64.0.0', '54.160.0.0','54.144.0.0',
'52.64.0.0','54.208.0.0','54.192.0.0',
'54.240.0.0','54.224.0.0','54.72.0.0',
'54.176.0.0','52.32.0.0','52.0.0.0',
'52.192.0.0','52.84.0.0','53.32.0.0']
def checkForAmazon():
if StartAddress in AmazonStartIPs:
Name = 'Amazon Web Services - Elastic Compute Cloud'
CountryCode = 'US'
AbuseEmail = '[email protected]'
AbusePhone = '+1-206-266-4064'
AbuseAddress = ['410 Terry Avenue','North Seattle', 'WA', '98109-5210','UNITED STATES']
iterateColumn = sheet.columns[2]#get all cell values in column C
currentRowIndex = 2
for Address in iterateColumn[1:5]:#test range 1:5 to reduce API load
ip_address = Address.value#set var to value of item in iterateColumn
IP = IPWhois(ip_address)#store whois call in var of IP
results = IP.lookup_rdap(depth=1)#call whois and store .json results
Name = results['network']['name']#set name to IP Host name
Name=''.join(Name)#formatting for excel
CountryCode = results['asn_country_code']#var for country code
CountryCode=''.join(CountryCode)
StartAddress = results['network']['start_address']#var for IP range Start
StartAddress=''.join(StartAddress)
EndAddress = results['network']['end_address']#var for IP range End
EndAddress = ''.join(EndAddress)
#write values above to iterable rows in spreadsheet
sheet.cell(row = currentRowIndex, column = 5).value = Name
sheet.cell(row = currentRowIndex, column = 6).value = CountryCode
sheet.cell(row = currentRowIndex, column = 7).value = StartAddress
sheet.cell(row = currentRowIndex, column = 8).value = EndAddress
for key in results['objects']:#get unique key values in results object
r = key#store as var of r to prevent having to call by ripe name
AbuseEmail = results['objects'][r]['contact']['email'][0]['value']
if results['objects'][r]['contact']['email'] == None:
AbuseEmail = 'NULL'
elif results['objects'] is None:
AbuseEmail = 'NULL'
sheet.cell(row = currentRowIndex, column = 9).value = AbuseEmail
AbuseEmail = ''.join(AbuseEmail)
if results['objects'][r]['contact']['phone'] == None:
AbusePhone = 'NULL'
else:
AbusePhone = results['objects'][r]['contact']['phone'][0]['value']
sheet.cell(row=currentRowIndex, column = 10).value = AbusePhone
AbusePhone = ''.join(AbusePhone)
if results['objects'][r]['contact']['address'] == None:
AbuseAddress = 'NULL'
else:
AbuseAddress = results['objects'][r]['contact']['address'][0]['value']
sheet.cell(row=currentRowIndex, column = 11).value = AbuseAddress
AbuseAddress =''.join(AbuseAddress)
checkForAmazon()
currentRowIndex += 1
rowsUpdated = sheet.max_row
print('{} records have been updated.'.format(rowsUpdated))
wb.save('ABUSE_IP_LOG_HOST_DATA.xlsx')
使用'try' /'except'每個條款可能存在重大錯誤的情況。 – MattDMo
我添加了嘗試/除了所有情況下,並解決了問題。謝謝MattDMo! – Fergus
我建議你分開執行查找的代碼,並將代碼轉換成可用的形式,並將其添加到工作表中。你正在使用的'sheet.cell(...)'是我非常想要阻止的事情。 –