如何使用 IPWhois 解析 python 中的文本

How to parse text in python with IPWhois

我的代码是:

from ipwhois import IPWhois
import pprint

obj = IPWhois('74.125.227.206')

results = obj.lookup_rws()

pprint.pprint(results)

它returns:

{'asn': '15169',
 'asn_cidr': '74.125.227.0/24',
 'asn_country_code': 'US',
 'asn_date': '2007-03-13',
 'asn_registry': 'arin',
 'nets': [{'abuse_emails': 'arin-contact@google.com',
           'address': '1600 Amphitheatre Parkway',
           'cidr': '74.125.0.0/16',
           'city': 'Mountain View',
           'country': 'US',
           'created': '2007-03-13T12:09:54-04:00',
           'description': 'Google Inc.',
           'handle': u'NET-74-125-0-0-1',
           'misc_emails': None,
           'name': 'GOOGLE',
           'postal_code': '94043',
           'range': u'74.125.0.0 - 74.125.255.255',
           'state': 'CA',
           'tech_emails': 'arin-contact@google.com',
           'updated': '2012-02-24T09:44:34-05:00'}],
 'query': '74.125.227.206',
 'raw': None}

python 从输出中打印一行的最佳或最简单方法是什么?

例如:

'name': 'GOOGLE', 

'abuse_emails': 'arin-contact@google.com',

如有任何帮助,我们将不胜感激!

results 已经是一个字典,所以只需获取您想要的键和值即可。

from ipwhois import IPWhois

obj = IPWhois('74.125.227.206')
results = obj.lookup_rws()
print(results['nets'][0]['name'])

lookup_rws() 函数已弃用。相反,使用:

from ipwhois import IPWhois

obj = IPWhois('{}'.format('IP') # Enter IP
result = obj.lookup_rdap(depth=1)
print(result['network']['name'])