我想列出一次我的脚本在python中搜索特定的字符串,但我也想添加国家代码的前两个字母,但当我尝试然后它说无效的KeyError:' country _ code ',但是api说我如何实现ocation.country_code?
#!/usr/bin/python
import shodan
SHODAN_API_KEY="xxxxxxxxxxxxxxxxxxxx"
api = shodan.Shodan(SHODAN_API_KEY)
try:
# Search Shodan
results = api.search('ProFTPd-1.3.3c')
# Show the results
for result in results['matches']:
print '%s' % result['ip_str']
print '%s' % result['country_code']
except shodan.APIError, e:
print 'Error: %s' % e发布于 2018-02-17 19:34:42
我认为这是您在Python https://github.com/achillean/shodan-python/blob/master/shodan/client.py#L324中使用的方法,它会触发:
return self._request('/shodan/host/search', args)Shodan文档:/shodan/host/search https://developer.shodan.io/api签出
我刚看到答案在你的问题上,但你吃了一封来自位置(定位)的信。
试试这个:
print '%s' % result['location']['country_code']所以你要找的字段在那里,但在另一本字典里。
我建议下次很好地阅读API文档,就像Nofal所说的那样,Python错误是不言自明的--如果您有dict上的KeyError --这意味着字段不存在。下一次听Python,它将揭示真相。
https://stackoverflow.com/questions/48844919
复制相似问题