IP查询接口及其简单使用方法(保持更新)

212次阅读
没有评论

需要用到 IP 查询 接口,发现原来的接口好多都不能用了,比如小气的腾讯。

这里分享几个目前能用的,大家各取所需吧。

纯真 IP 地址库

可以本地查询,如果查询量特别大且对速度要求比较高的话,推荐用这个。

#!/usr/bin/env bash
# 下载纯真 IP 地址库,可以设置一个 crontab 定时任务,定期更新

for i in $(seq 1 60); do
  if wget -O QQWry.dat.new https://kgithub.com/gai871013/ip-location/raw/master/src/QQWry.dat; then
    f_size=$(wc -c QQWry.dat.new|awk '{print $1}')
    if [$f_size -gt $((1024*1024*5)) ]; then
      mv -f QQWry.dat.new QQWry.dat
      break
    fi
  fi
  sleep 60
done

查询方法:

from qqwry import QQwry
from log import logger

# pip install qqwry-py3==1.2.1

class QQWRY(object):
    def __init__(self):
        self.q = QQwry()
        self.q.load_file('QQWry.dat', loadindex=True)

    def get_ip_info(self, ip):
        """
        获取 IP 地址信息
        :param ip:
        :return:
        (' 上海市 ', ' 电信 ')
        (' 贵州省贵阳市 ', ' 电信 ')
        (' 美国加利福尼亚州圣克拉拉县山景市 ', ' 谷歌公司 DNS 服务器 ')
        """
        # url = f"https://opendata.baidu.com/api.php?query={ip}&co=&resource_id=6006&oe=utf8"
        # url = f"https://api.vore.top/api/IPdata?ip={ip}"
        # r = s.get(url)
        # sleep(1)
        try:
            ip_info = self.q.lookup(ip)
            logger.debug(f"get ip information: {ip_info}")
            return ip_info
        except Exception as e:
            logger.error(f"get ip information failed, error: {e}")
            return ('', '')
    # try:
    #     ipdata = r.json().get('ipdata', {})
    #     return (ipdata.get('info1', ' 未知省份 '), ipdata.get('info2', ' 未知城市 '), ipdata.get('isp', ' 未知运营商 '))
    # except Exception as e:
    #     logger.error(f"get ip information failed, {ip}, response: {r.text}")
    #     return (' 获取 IP 信息失败 ', ' 未知城市 ', ' 未知运营商 ')

ip_queryer = QQWRY()

if __name__ == '__main__':
    print(ip_queryer.get_ip_info('114.114.114.114'))

太平洋 IP 地址库 API 接口

*. 请求接口(GET):

http://whois.pconline.com.cn/?ip=[ip 地址]
http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=[ip 地址]

调用方法非常丰富,提供多种 JS 嵌入调用。

*. 返回示例 Json

{"ip":"61.235.82.163","pro":" 广东省 ","proCode":"440000","city":" 广州市 ","cityCode":"440100","region":" 天河区 ","regionCode":"440106","addr":" 广东省广州市天河区 蓝色心情网吧 ","regionNames":"","err":""}

聚合数据 IP 地址接口

每天免费 50 次查询,介绍:https://www.juhe.cn/docs/api/id/1

*. 请求接口:

http://apis.juhe.cn/ip/ipNewV3?ip=[ip 地址]

*. 返回示例 Json

{"resultcode":"200","reason":" 查询成功 ","result":{"Country":" 中国 ","Province":" 广东 ","City":" 广州 ","District":"","Isp":" 铁通 "},"error_code":0}

api.vore.top 查询接口

没有频率限制,自动区分 IPv4 和 IPv6

*. 请求接口(GET):

https://api.vore.top/api/IPdata?ip=[ip 地址]

*. 请求示例

https://api.vore.top/api/IPdata?ip=117.136.12.79
https://api.vore.top/api/IPdata?ip=2409:895c:8a04:32f5:c5dc:a521:ba06:cf56

*. 返回示例 Json


{"code":200,"msg":"SUCCESS","ipinfo":{"type":"ipv6","text":"2409:895c:3204:50a2:4dc2:1d06:8ed7:6148","cnip":false},"ipdata":{"info1":" 广西区 ","info2":" 桂林市 ","info3":" 七星区 ","isp":" 移动 "},"adcode":{"o":" 广西区桂林市七星区 - 移动 ","p":" 广西区 ","c":" 桂林 ","n":" 广西区 - 桂林 ","r":null,"a":null,"i":false},"tips":" 接口由 VORE-API(https:\/\/api.vore.top\/)免费提供 ","time":1689758892}

api.mir6.com 查询接口

自动区分 IPv4 和 IPv6、域名,介绍:https://api.mir6.com/doc/ip_json.html

*. 请求接口(GET):

https://api.mir6.com/api/ip_json?ip=[ip 地址]

*. 请求示例

https://api.vore.top/api/IPdata?ip=117.136.12.79
https://api.vore.top/api/IPdata?ip=2409:895c:8a04:32f5:c5dc:a521:ba06:cf56
https://api.mir6.com/api/ip_json?ip=www.qq.com

*. 返回示例 Json

{"code":200,"msg":"success","data":{"ip":"240e:97c:2f:3003::77","dec":"47925092370312089887601455778723004535","country":" 中国 ","countryCode":"CN","province":" 广东省 ","city":" 广州市 ","districts":"","idc":"","isp":" 中国电信 ","net":" 数据中心 ","zipcode":"510000","areacode":"020","protocol":"IPv6","location":" 中国[CN] 广东省 广州市 ","myip":"222.137.131.250","time":"2023-07-19 17:33:46"}}

ip-api.com 接口

官网:https://ip-api.com/docs/api:json

*. 请求接口(GET):

http://ip-api.com/json/[ip 地址]
http://ip-api.com/json/[ip 地址]?lang=zh-CN

*. 返回示例 Json

{"status":"success","country":"China","countryCode":"CN","region":"GD","regionName":"Guangdong","city":"Xiaolou","zip":"","lat":23.379,"lon":113.763,"timezone":"Asia/Hong_Kong","isp":"China Railway Telecommunications","org":"","as":"AS38370 China TieTong Telecommunications Corporation","query":"61.235.82.163"}

若是在接口添加了 ?lang=zh-CN 则返回如下

{"status":"success","country":" 中国 ","countryCode":"CN","region":"GD","regionName":" 广东 ","city":" 小楼 ","zip":"","lat":23.379,"lon":113.763,"timezone":"Asia/Hong_Kong","isp":"China Railway Telecommunications","org":"","as":"AS38370 China TieTong Telecommunications Corporation","query":"61.235.82.163"}

淘宝 IP 地址库【已失效】

*. 网址:http://ip.taobao.com/

*. 请求接口(GET):

http://ip.taobao.com/service/getIpInfo.php?ip=[ip 地址]

*. 返回数据格式:(json 格式的)国家、省(自治区或直辖市)、市(县)、运营商。例如:

{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}

注:其中 code 的值的含义为,0:成功,1:失败。

*.  淘宝 IP 接口使用示例 PHP 版:

<?php
$ip=get_ip_data();
function get_ip_data(){$ip=file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip());
    $ip = json_decode($ip);
    if($ip->code){return false;}
    $data = (array) $ip->data;
    print_r($data);//return $data;
}

// 取客户端 ip
function get_client_ip(){if (isset($_SERVER['HTTP_CLIENT_IP']) and !empty($_SERVER['HTTP_CLIENT_IP'])){return $_SERVER['HTTP_CLIENT_IP'];
    }
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR'])){return strtok($_SERVER['HTTP_X_FORWARDED_FOR'], ',');
    }
    if (isset($_SERVER['HTTP_PROXY_USER']) and !empty($_SERVER['HTTP_PROXY_USER'])){return $_SERVER['HTTP_PROXY_USER'];
    }
    if (isset($_SERVER['REMOTE_ADDR']) and !empty($_SERVER['REMOTE_ADDR'])){return $_SERVER['REMOTE_ADDR'];
    } else {return "0.0.0.0";}
}
?>

新浪 IP 地址查询接口【已失效】

*. 请求接口(GET):

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=[ip 地址]

注:参数 format 可设置返回格式:js/json;参数 ip 可选查询 IP;

*.  返回格式如下:

var remote_ip_info = {"ret":1,"start":"125.40.0.0","end":"125.40.111.255","country":"\u4e2d\u56fd","province":"\u6cb3\u5357","city":"\u90d1\u5dde","district":"","isp":"\u8054\u901a","type":"","desc":""};

下面的实例用到了 JQuery 下的 getScript()方法

<div id="sina_ip_info"></div>
<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
var uip = '8.8.8.8';
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='+uip, function(_result){
    var ipData = ""; // 初始化保存内容变量
    if (remote_ip_info.ret == '1'){
        ipData += "IP 详细信息:<br>";
        ipData += "IP:" + uip + "<br>";
        ipData += " 国家:" + remote_ip_info.country + "<br>";
        ipData += " 省份:" + remote_ip_info.province + "<br>";
        ipData += " 城市:" + remote_ip_info.city + "<br>";
        ipData += " 区:" + remote_ip_info.district + "<br>";
        ipData += "ISP:" + remote_ip_info.isp + "<br>";
        ipData += " 类型:" + remote_ip_info.type + "<br>";
        ipData += " 其他:" + remote_ip_info.desc + "<br>";
        $("#sina_ip_info").html(ipData); // 显示处理后的数据
    } else {alert(' 错误 ', ' 没有找到匹配的 IP 地址信息!');
    }
});
</script>

搜狐 IP 地址查询接口(默认编码 GBK)

*. 请求接口(GET):

http://pv.sohu.com/cityjson?ie=utf-8

稳定付费的接口

聚合数据

https://www.juhe.cn/docs/api/id/1

阿里云

云市场,价格不贵

https://market.aliyun.com/

站长之家

https://www.chinaz.net/mall/a_T9wERBm9NZ.html

参考来源

  1. IP 地址查询接口 2021 版
  2. IP 查询接口及其简单使用方法(保持更新)– YangJunwei
正文完
 
评论(没有评论)