我正在尝试从IPWhois
查找中提取数据并将其放入list
。我不知道在results2[7]
from ipwhois import IPWhois
import pprint
obj = IPWhois('74.125.227.206')
results = []
results = obj.lookup_rws()
print '**********************************'
print 'PPrint Result:'
pprint.pprint(results)
print ''
print '**********************************'
results2 = results.items()
print '**********************************'
print 'After putting them into lists: '
print ''
print results2[0]
print results2[1]
print results2[2]
print results2[3]
print results2[4]
print results2[5]
print results2[6]
print results2[7]
print '**********************************'
结果:
C:\Python27\Scripts>python whois.py
**********************************
PPrint Result:
{'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}
**********************************
**********************************
After putting them into lists:
('asn_registry', 'arin')
('asn_date', '2007-03-13')
('asn_country_code', 'US')
('raw', None)
('asn_cidr', '74.125.227.0/24')
('query', '74.125.227.206')
('nets', [{'city': 'Mountain View', 'updated': '2012-02-24T09:44:34-05:00', 'han
dle': u'NET-74-125-0-0-1', 'description': 'Google Inc.', 'tech_emails': 'arin-co
ntact@google.com', 'country': 'US', 'abuse_emails': 'arin-contact@google.com', '
created': '2007-03-13T12:09:54-04:00', 'range': u'74.125.0.0 - 74.125.255.255',
'state': 'CA', 'postal_code': '94043', 'address': '1600 Amphitheatre Parkway', '
cidr': '74.125.0.0/16', 'misc_emails': None, 'name': 'GOOGLE'}])
('asn', '15169')
*********************************
Inteded结果也是将results2[7]
中的项目放在它自己的列表中。由于他们是('nets', [{
的导致我不确定如何接近,所以链条中的扳手是分开的。
我试过这个:
from ipwhois import IPWhois
import pprint
obj = IPWhois('74.125.227.206')
results = []
results = obj.lookup_rws()
print '**********************************'
print 'PPrint Result:'
pprint.pprint(results)
print ''
print '**********************************'
results2 = results.items()
results3 = results[7]
results4 = results3.items()
print '**********************************'
print 'After putting them into lists: '
print ''
print results2[0]
print results2[1]
print results2[2]
print results2[3]
print results2[4]
print results2[5]
print results2[6]
print results2[7]
print '**********************************'
print results4
结果:
C:\Python27\Scripts>python whois.py
**********************************
PPrint Result:
{'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}
**********************************
Traceback (most recent call last):
File "whois.py", line 13, in <module>
results3 = results[7]
KeyError: 7
答案 0 :(得分:0)
如果你想用元组而不是字典打印东西可能会有所帮助。
from ipwhois import IPWhois
import pprint
obj = IPWhois('74.125.227.206')
results = []
results = obj.lookup_rws()
print '**********************************'
print 'PPrint Result:'
pprint.pprint(results)
print ''
print '**********************************'
results2 = results.items()
print '**********************************'
print 'After putting them into lists: \n'
for item in results2:
if item[0] == 'nets':
temp_result = []
for stuff in item[1]:
temp_result.append(item[1].items())
else:
temp_result = item[1]
print temp_result
print '**********************************'