“'NoneType'对象不可订阅”或“KeyError:”使用openpyxl和ipwhois

时间:2016-06-18 01:26:41

标签: python-3.x openpyxl nonetype whois keyerror

我正在处理执行以下操作的python3脚本:

  1. 在工作目录中打开Excel文件
  2. 选择Excel文件中的第一张工作表
  3. 选择第三列中的所有数据(在本例中为一系列IP地址)
  4. 遍历所有IP地址并为每个
  5. 调用whois API
  6. 将每个IP的结果存储在变量(.json)
  7. 通过搜索结果查找名称,IP范围,联系信息
  8. 将上述6中的值写入excel文件中的新行
  9. 使用当前目录中的新名称保存excel文件
  10. 当前文档包含427个唯一IP地址的列表,来自RIPI名称的whois api的结果是唯一的(有时在同一响应中)。为了适应这种情况,我迭代了每个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 = 'abuse@amazonaws.com'
          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')
    

1 个答案:

答案 0 :(得分:0)

根据MattDMo的建议,我添加了以下异常处理并解决了问题:

try:
   AbuseEmail = results['objects'][r]['contact']['email'][0]['value']
   AbusePhone = results['objects'][r]['contact']['phone'][0]['value']
   AbuseAddress = results['objects'][r]['contact']['address'][0]['value']
except (KeyError, TypeError):
   AbuseEmail = 'NULL'
   AbusePhone = 'NULL'
   AbuseAddress = 'NULL'