import base64
import time
import pandas as pd
import requests
# API 查询语句示例
API_PROMPT = """
web.title="北京" # 从网站标题中搜索“北京”
ip.country="CN" # 搜索IP对应主机所在国为”中国“的资产
icp.web_name="奇安信" # 搜索ICP备案网站名中含有“奇安信”的资产
icp.name="奇安信" # 搜索ICP备案单位名中含有“奇安信”的资产
domain="qq.com" # 搜索域名包含"qq.com"的网站
domain.suffix="qq.com" # 搜索主域为qq.com的网站
app.vendor="PHP"NEW # 查询包含组件厂商为"PHP"的资产
app.version="1.8.1"NEW # 查询包含组件版本为"1.8.1"的资产
header="elastic" # 搜索HTTP请求头中含有"elastic"的资产
header.status_code="402" # 搜索HTTP请求返回状态码为”402”的资产
web.body="网络空间测绘" # 搜索网站正文包含”网络空间测绘“的资产
header.server=="Microsoft-IIS/10" # 搜索server全名为“Microsoft-IIS/10”的服务器
ip_address="1.1.1.1" # 搜索IP为 ”1.1.1.1”的资产
ip_range="220.181.111.1/24" # 搜索起始IP为”220.181.111.1“的C段资产
ip.port="6379" # 搜索开放端口为”6379“的资产
ip.os="Windows" # 搜索操作系统标记为”Windows“的资产
ip.isp="电信" # 搜索运营商为”中国电信“的资产
"""
# 定义API URL和密钥
API_URL_BASE = "https://hunter.qianxin.com/openApi/search?api-key="
API_KEY = ""
def fetch_data(query, page_count):
try:
query_b64_encoded = base64.b64encode(query.encode('utf-8')).decode('utf-8')
results_list = []
page_size = 10
for page in range(1, int(page_count) + 1):
url = f"{API_URL_BASE}{API_KEY}&search={query_b64_encoded}&page={page}&page_size={page_size}&is_web=1&status_code=200"
response_json = requests.get(url).json()
print(url)
if response_json['code'] != 200:
print(f"请求失败,URL: {url}")
print(response_json.text)
exit()
print(response_json['data']['rest_quota'])
results_list.append(response_json)
time.sleep(3) # 遵守服务端请求间隔限制
return results_list
except:
print('查询语法有问题')
def export_to_excel(json_response_list, search_query):
data_rows = []
for index, json_data in enumerate(json_response_list):
def extract_and_add(data):
# 检查 'arr' 是否存在且非空
if 'data' in data and data['data'] is not None and 'arr' in data['data'] and data['data']['arr'] is not None:
for item in data['data']['arr']:
data_rows.append(item)
else:
print(f"警告:数据 {index} 中不存在可迭代的资产数组")
try:
extract_and_add(json_data)
except TypeError as te:
print(f"在处理响应 {index} 时遇到错误: {te}")
dataframe = pd.DataFrame(data_rows)
# 使用查询字符串生成文件名,移除特殊字符
file_name_safe = search_query.replace(" ", "_").replace('"', '') + ".xlsx"
dataframe.to_excel(file_name_safe, index=False)
if __name__ == '__main__':
print(API_PROMPT)
search_query = input("请输入查询语句:")
number_of_pages = input("输入获取的数量(页数):")
responses = fetch_data(search_query, number_of_pages)
export_to_excel(responses, search_query)
鹰图API
最新推荐文章于 2025-03-19 17:28:52 发布