我目前正在学习python,并试图找到一种方法来编译来自IP注册的数据,但我偶然发现了嵌套的字典/列表。我找到了一种提取所需数据的方法,但这似乎过多,但我所有合并迭代的尝试均失败了。有关此主题的任何文档或一些指针将不胜感激。
from ipwhois import IPWhois
obj = IPWhois('8.8.8.8')
results = obj.lookup_rdap(depth=1)
for key, val in results.items():
if isinstance(val, dict):
if key == 'objects':
objects = (val)
for key, val in objects.items():
if isinstance(val, dict) and key.startswith("ABUSE"):
abuse =(val)
for key, val in abuse.items():
if isinstance(val, dict):
contact = (val)
for key,val in contact.items():
if isinstance(val, list):
if key == 'email':
email = (val)
value=(email[0])
print (value['value'])
答案 0 :(得分:0)
我认为这很清楚,您应该对obj的格式有个很好的了解
from ipwhois import IPWhois
obj = IPWhois('8.8.8.8')
results = obj.lookup_rdap(depth=1)
objects = results['objects']
for key, val in objects.items():
if not key.startswith('ABUSE'):
continue
for ele in val['contact']['email']:
print(ele['value'])