腳本利用強(qiáng)大ZoomEye API批量查詢,但是對于一般開發(fā)者的查詢有限制葱蝗,
限制條件
為了防止惡意使用 ZoomEye 平臺的資源喳资,構(gòu)建良好網(wǎng)絡(luò)研究生態(tài)鲁猩。 我們對資源設(shè)置了一定限制條件,保障整個(gè)社區(qū)的健康使用岖沛。
對外開放的開發(fā)者版本暑始,只能獲取總結(jié)果的 30%,同時(shí)涵蓋 100,000 結(jié)果條數(shù)上限婴削。
希望小伙伴們能夠合理利用該腳本廊镜。
#!/usr/bin/env python
# coding:utf-8
import json
import sys
import urllib
from time import sleep
import requests as req
reload(sys)
sys.setdefaultencoding('utf-8')
class ZoomEye(object):
"""利用ZoomEye的API進(jìn)行批量搜索,并將結(jié)果保存到本地
"""
def __init__(self, host="api.zoomeye.org"):
self._base_uri = "http://%s" % host
self._headers = {"Authorization" : "JWT %s" % self.get_token(),
"Content-Type": "application/json"}
def get_token(self):
"""
獲取ZoomEye的授權(quán)token
"""
# payload = {"username": "你的ZoomEye賬號", "password": "你的ZoomEye密碼"}
try:
res = req.post('https://api.zoomeye.org/user/login',
data=json.dumps(payload))
except Exception as e:
print e
sys.exit()
return json.loads(res.text)['access_token']
def resource_info(self):
"""
查看當(dāng)月還有多少剩余查詢結(jié)果
"""
return req.get("https://api.zoomeye.org/resources-info", headers=self._headers).content
def get_content(self, searchtype, keyword, startpage, endpage):
'''
獲取查詢內(nèi)容
'''
for i in xrange(startpage, endpage + 1):
print "Get page " + str(i) + " info ..."
uri = 'https://api.zoomeye.org/%s/search?query=%s&page=%s&fact=app,os' % (
searchtype, urllib.quote(keyword), i)
try:
result_page = req.get(uri, headers=self._headers)
page_content = json.loads(result_page.content)
except Exception as e:
print e
print type(page_content)
if result_page.status_code == 200:
# 搜索有兩種類型唉俗,一種是host嗤朴,另一種是web服務(wù)
if searchtype == 'host':
for match in page_content['matches']:
# print match
# 整理host搜索結(jié)果配椭,這里可以自己看返回包自定義
res_line = match['ip'] + ':' + str(match['portinfo']['port']) + '\t' + match[
'portinfo']['banner'].strip() + '\t' + match['geoinfo']['isp']
print res_line
self.save_result(res_line)
elif searchtype == 'web':
for match in page_content['matches']:
# 整理web搜索結(jié)果,這里可以自己看返回包自定義
res_line = match['ip'][
0] + '\t' + match['title'] + '\t' + 'http://' + match['site']
print match['ip'][0] + '\t' + 'http://' + match['site']
self.save_result(str(res_line).encode('utf-8'))
else:
print "Error Code: %s" % result_page.status_code, result_page.content
sleep(0.2)
def search(self, keyword, page=1, searchtype="web"):
'''執(zhí)行查詢操作'''
uri = 'https://api.zoomeye.org/%s/search?query=%s&page=%s&fact=app,os' % (
searchtype, urllib.quote(keyword), page)
pages = self.get_pages(uri)
# 執(zhí)行查詢操作
self.get_content(searchtype=searchtype, keyword=keyword,
startpage=page, endpage=pages)
def save_result(self, res):
'''將結(jié)果寫入文件'''
with open('result.txt', 'a') as f:
f.writelines(res + '\n')
def get_pages(self, uri):
'''對搜索結(jié)果進(jìn)行分頁'''
try:
response = req.get(uri, headers=self._headers)
search_res = json.loads(response.content)
print response.url
except Exception as e:
print e
# 此處判斷有無搜索到結(jié)果雹姊,如果響應(yīng)代碼為200表示請求到結(jié)果并返回頁數(shù)股缸,否則打印錯(cuò)誤提示
if response.status_code == 200:
total = int(search_res['total'])
page = total / 10
if total % 10 == 0:
return page
print page
return page + 1
else:
print "Error Code: ", response.status_code, "Tips: ", search_res
sys.exit()
if __name__ == "__main__":
zoomeye = ZoomEye()
# 查看當(dāng)月還有多少剩余查詢結(jié)果
print zoomeye.resource_info()
# 查詢
print zoomeye.search("city:jinan struts2", searchtype="web", page=1)
腳本使用說明
主機(jī)查詢字段
https://www.zoomeye.org/api/doc#search-filters
web查詢字段
https://www.zoomeye.org/api/doc#web-search
Windows下最好右鍵使用IDLE打開該腳本按F5執(zhí)行
如有報(bào)錯(cuò)歡迎截圖留言,我將不斷完善吱雏。
github下載地址:https://github.com/ucstone/codelib