域名查询接入点详解:域名 → IP → 归属地(20-2)
# 域名查询接入点详解:域名 → IP → 归属地(20-2)
> 元信息:接口 全球IP地址查询(apiCode=20)· 接入点 20/2 · 免费 · POST/GET · JSON · 适用人群 开发者/运维 · 阅读时间 5 分钟
## TL;DR 快速概览
- 接入点 `20/2` 是"域名查询":给一个 `domain`(如 `www.baidu.com`),接口先解析出 IP,再返回该 IP 的归属地。
- 唯一必填业务参数:`domain`(String);接口地址 `https://route.showapi.com/20-2?appKey={your_appKey}`。
- 返回里多一个 `ip` 字段(域名解析出的真实 IP),其余归属地字段与 20/1 一致。
## Why:域名查询解决什么
网络管理、安全监测、CDN/域名解析排障时,你手里常是域名而非 IP。手动 `nslookup` 再查归属地很碎;`20/2` 一步到位:域名 → IP → 归属地,适合"这个域名到底部署在哪"的快速判断。
## What:20/2 接入点速览
| 项 | 内容 |
|----|------|
| 接入点 | `20/2` 域名查询 |
| 接口地址 | `https://route.showapi.com/20/2?appKey={your_appKey}` |
| 必填参数 | `domain`(String,如 `www.baidu.com`) |
| 返回 | `ip`(解析出的 IP)+ 归属地字段(`country`/`region`/`city`/`isp`/`lnt`/`lat`/`city_code`/`continents`/`en_name`/`en_name_short` 等) |
## How:调用 20/2
**cURL**
```bash
curl -X POST "https://route.showapi.com/20-2?appKey=YOUR_APPKEY" \
-H "content-type: application/x-www-form-urlencoded" \
-d "domain=www.baidu.com"
```
**Python**
```python
import requests
def query_domain(domain, appkey="YOUR_APPKEY"):
resp = requests.get(
"https://route.showapi.com/20-2",
params={"appKey": appkey, "domain": domain},
timeout=10,
)
data = resp.json()
if data.get("showapi_res_code") != 0:
return None, data.get("showapi_res_error")
body = data["showapi_res_body"]
if body.get("ret_code") != "0":
return None, f"业务失败 ret_code={body.get('ret_code')}"
return body, None
body, err = query_domain("www.baidu.com")
if err:
print("失败:", err)
else:
print("解析IP:", body.get("ip"),
"归属地:", body.get("country"), body.get("region"), body.get("city"))
```
**Node.js**
```javascript
async function queryDomain(domain, appkey = "YOUR_APPKEY") {
const resp = await fetch(
`https://route.showapi.com/20-2?appKey=${appkey}&domain=${domain}`
);
const data = await resp.json();
if (data.showapi_res_code !== 0) return { err: data.showapi_res_error };
const body = data.showapi_res_body;
if (body.ret_code !== "0") return { err: `业务失败 ret_code=${body.ret_code}` };
return { body };
}
```
## 返回示例与解析
```json
{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": {
"region": "北京", "isp": "电信", "en_name": "China",
"country": "中国", "city": "北京", "ip": "220.181.112.244",
"ret_code": 0, "county": "", "continents": "亚洲",
"city_code": "110100", "lnt": "116.405285",
"en_name_short": "CN", "lat": "39.904989"
}
}
```
> 注意:文档返回体表格只列了 `ip/country/city/county/isp/region`,但**真实返回含完整地理字段集**(`continents`/`en_name`/`en_name_short`/`city_code`/`lnt`/`lat`/`ret_code`),与 20/1 一致;`area`(片区) 在 20/2 示例中未出现。解析代码按"字段可能缺失"用 `.get()` 容错。
## 进阶 / 边界
- **一个域名可能解析多个 IP**:接口返回的是其解析出的一个 IP 对应的归属地,不能代表该域名全部节点。做全面判断时自行 `nslookup` 取多 IP 后逐个查。
- **域名要带主机名**:传 `www.baidu.com` 而非裸 `baidu.com` 更易解析到具体 IP;具体以返回 `ip` 为准。
- **坐标系未声明**:`lnt`/`lat` 为近似位置,详见[坐标标点指南](https://www.showapi.com/guides/ip-geo-coordinate-map-20)。
## FAQ
**Q1:domain 必填吗?**
A:是。20/2 唯一必填业务参数是 `domain`,不传返回业务失败。
**Q2:为什么返回里多了 `ip` 字段?**
A:`20/2` 先解析域名得到 IP,再把该 IP 的归属地一并返回,所以比 20/1 多一个 `ip` 字段,其余归属地字段含义相同。
**Q3:能查境内外域名吗?**
A:可。返回的 `country`/`continents`/`en_name` 会反映域名解析 IP 的实际地理位置,海外域名照常返回。
**Q4:文档表格字段比示例少,以哪个为准?**
A:以实际返回 JSON 为准(含 continents/en_name 等)。这是文档返回体表格的遗漏,已在[返回字段全解](https://www.showapi.com/guides/ip-geo-response-fields-20)中补全。
## 相关能力 / 下一步阅读
- [全球IP地址查询接入点详解:IP → 归属地定位(20-1)](https://www.showapi.com/guides/ip-geo-ip-query-20)
- [全球IP地址查询:返回字段全解](https://www.showapi.com/guides/ip-geo-response-fields-20)
- [全球IP地址查询:如何用 lnt/lat 在地图上标点](https://www.showapi.com/guides/ip-geo-coordinate-map-20)
- **本系列共 10 篇**:查看[全球IP地址查询指南总目录](https://www.showapi.com/guides/ip-geo-guides-20)