IP地理位置查询API收集
日前在跟进一个提供ip查询的内部项目,主数据库用的ipip,在查询页面同时提供一些第三方提供查询结果,收集了部分api
淘宝
推荐度:5星
url: "http://ip.taobao.com/service/getIpInfo.php?ip="+ip
返回json格式数据,code
字段为0表示查询成功,主要地理位置数据包括’country’, ‘area’, ‘region’, ‘city’, ‘isp’,请求频率有限制,应该是不大于10ps
例程:python
url = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip
try:
r = requests.get(url, timeout=5)
except Exception, e:
result = {
"error": 1,
"msg": '请求失败'
}
return result
if r.status_code == requests.codes.ok:
data = r.json()
if data['code'] == 0:
t = data.get('data')
msg = [t.get('country',''), t.get('area',''), t.get('region',''), t.get('city',''), t.get('isp','')]
result = {
"error": data['code'],
"msg": ' '.join(msg),
"thirdpart": '淘宝'
}
新浪
推荐度:4星
url: "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip
返回包含javascript语句的字符串,在jquery环境下,可直接用getScript()函数获取和赋值。主要地理位置数据包括’country’, ‘province’, ‘city’, ‘isp’
例程: python
url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip
try:
r = requests.get(url, timeout=5)
except Exception, e:
result = {
"error": 1,
"msg": '请求失败'
}
return result
if r.status_code == requests.codes.ok:
data = r.text
content = data.split('=')[-1].strip()[:-1]
try:
t = simplejson.loads(content)
except Exception, e:
result = {
"error": 1,
"msg": '请求失败'
}
return result
msg = [t.get('country',''), t.get('province',''), t.get('city',''), t.get('isp','')]
result = {
"error": 0,
"msg": ' '.join(msg),
"thirdpart": '新浪'
}
太平洋IP地址库(pconline)
推荐度:3星
url: "http://whois.pconline.com.cn/ip.jsp?ip="+ip
直接返回地理位置字符串,当然可以通过调用不同接口返回不同格式数据,详看这里
例程:python
url = "http://whois.pconline.com.cn/ip.jsp?ip="+ip
try:
r = requests.get(url, timeout=5)
except Exception, e:
result = {
"error": 1,
"msg": '请求失败'
}
return result
if r.status_code == requests.codes.ok:
data = r.text
result = {
"error": 0,
"msg": data,
"thirdpart": 'pconline'
}
ip138
推荐度:4星
url: "http://test.ip138.com/query/?ip="+ip
同样返回包含地理位置信息的字符串
例程:python
url = "http://test.ip138.com/query/?ip="+ip
try:
r = requests.get(url, timeout=5)
except Exception, e:
result = {
"error": 1,
"msg": '请求失败'
}
return result
if r.status_code == requests.codes.ok:
data = r.json()
if data.get("ret") == "ok":
msg = data.get("data")
result = {
"error": 0,
"msg": ' '.join(msg),
"thirdpart": 'ip138'
}
GeoIp
url: "http://geoip.nekudo.com/api/%s/zh" % ip
GeoIp有离线版数据库可以下载,提供了多个语言的调用方法,包括了python,看这里
如果不想使用离线版,这个网站提供了api,返回json格式数据.
例程:python
url = "http://geoip.nekudo.com/api/%s/zh" % ip
try:
r = requests.get(url, timeout=5)
except Exception, e:
result = {
"error": 1,
"msg": '请求失败'
}
return result
if r.status_code == requests.codes.ok:
data = r.json()
msg = [data.get("country",[]).get("name") or '', data.get("city") or '']
result = {
"error": 0,
"msg": ' '.join(msg),
"thirdpart": 'GeoIp'
}