Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse text in python with IPWhois

My code is:

from ipwhois import IPWhois
import pprint

obj = IPWhois('74.125.227.206')

results = obj.lookup_rws()

pprint.pprint(results)

It 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': '[email protected]',
           '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': '[email protected]',
           'updated': '2012-02-24T09:44:34-05:00'}],
 'query': '74.125.227.206',
 'raw': None}

What is the best or easiest way in python to print a single line from the output?

For example:

'name': 'GOOGLE', 

or 'abuse_emails': '[email protected]',

Any help would be appreciated!!

like image 652
gamarga Avatar asked Mar 13 '26 10:03

gamarga


2 Answers

results is already a dictionary, so just get the keys and values that you want.

from ipwhois import IPWhois

obj = IPWhois('74.125.227.206')
results = obj.lookup_rws()
print(results['nets'][0]['name'])
like image 197
Celeo Avatar answered Mar 15 '26 00:03

Celeo


lookup_rws() function is deprecated. Instead, use:

from ipwhois import IPWhois

obj = IPWhois('{}'.format('IP') # Enter IP
result = obj.lookup_rdap(depth=1)
print(result['network']['name'])
like image 43
ic205 Avatar answered Mar 15 '26 01:03

ic205



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!