如何在Python中使用IPWhois解析文本

3

我的代码如下:

from ipwhois import IPWhois
import pprint

obj = IPWhois('74.125.227.206')

results = obj.lookup_rws()

pprint.pprint(results)

它返回:

{'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' 的意思是“滥用邮件”或者“投诉邮件”,可以发送到 arin-contact@google.com。希望这能对你有所帮助!
2个回答

6

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

from ipwhois import IPWhois

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

2

lookup_rws()函数已被弃用。请使用以下函数代替:

from ipwhois import IPWhois

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

使用“from pprint import pprint”和“pprint(result)”来显示结果中的所有键和值。 - Nelbren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接