通过调用搜索引擎开放API或第三方收录查询接口,可批量检测域名的收录状态。本文提供Python和Java两种语言的可直接运行的代码示例,覆盖参数构造、批量请求、结果解析全流程。
接口选择
搜索引擎API:Bing Web Search API、Google Custom Search API,使用 site:example.com 查询语法API接口批量查询域名收录代码示例,返回收录结果数。
第三方收录查询API:站长工具开放平台、5118等,直接返回百度/搜狗等引擎的收录量数值API接口批量查询域名收录代码示例,适合国内域名。
以下示例以通用REST接口为准,替换为实际接口地址和认证方式即可。
Python代码示例
import requests, time
API_URL = "https://api.example.com/v1/domain/index"
HEADERS = {"Authorization": "Bearer YOUR_API_KEY"}
DOMAINS = ["a.com", "b.com", "c.com"] # 批量域名列表
results = []
for i, domain in enumerate(DOMAINS, 1):
resp = requests.get(API_URL, params={"domain": domain}, headers=HEADERS, timeout=10)
data = resp.json()
results.append({
"domain": domain,
"indexed": data.get("count", 0),
"status": resp.status_code
})
if i % 50 == 0:
time.sleep(1) # 每50次请求暂停1秒,避免触发限流
print(f"[{i}/{len(DOMAINS)}] {domain} → 收录 {data.get('count', 0)}")
with open("收录结果.json", "w", encoding="utf-8") as f:
f.write(str(results))
Java代码示例
import java.net.http.;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.;
import java.util.concurrent.*;
public class BatchDomainCheck {
private static final String API_URL = "https://api.example.com/v1/domain/index";
private static final String API_KEY = "YOUR_API_KEY";
public static void main(String[] args) throws Exception {
List domains = List.of("a.com", "b.com", "c.com");
HttpClient client = HttpClient.newHttpClient();
ExecutorService pool = Executors.newFixedThreadPool(5);
List> futures = new ArrayList<>();
for (String domain : domains) {
String url = API_URL + "?domain=" + domain;
futures.add(pool.submit(() -> {
HttpRequest req = HttpRequest.newBuilder()
.uri(java.net.URI.create(url))
.header("Authorization", "Bearer " + API_KEY)
.GET().build();
HttpResponse resp = client.send(req, BodyHandlers.ofString());
return domain + " → " + resp.body();
}));
}
for (Future f : futures) {
System.out.println(f.get(10, TimeUnit.SECONDS));
}
pool.shutdown();
}
}
关键注意事项
常见问题
Q:返回收录数为0是未收录还是接口异常?
A:先检查HTTP状态码。200且count为0表示该域名确无收录;非200或超时则需重试,不能直接判定为未收录。
Q:能否同时查询百度、Google、Bing多引擎?
A:可以。为每个引擎分别构造请求参数(如 engine=baidu),循环遍历引擎列表,结果按"域名+引擎"二维存储即可。
