快速获取IP地址的各种方法

时间:2023-03-10 04:51:54
快速获取IP地址的各种方法

调用百度的IP定位API(首先需要去百度开放平台注册申请key值http://lbsyun.baidu.com/apiconsole/key)

http://api.map.baidu.com/location/ip?ip=xx.xx.xx.xx&ak=您的AK&coor=bd09ll

https://api.map.baidu.com/location/ip?ip=xx.xx.xx.xx&ak=您的AK&coor=bd09ll

参数

快速获取IP地址的各种方法

返回结果示例:

{
address: "CN|北京|北京|None|CHINANET|1|None", #详细地址信息
content: #结构信息
{
address: "北京市", #简要地址信息
address_detail: #结构化地址信息
{
city: "北京市", #城市
city_code: 131, #百度城市代码
district: "", #区县
province: "北京市", #省份
street: "", #街道
street_number: "" #门牌号
},
point: #当前城市中心点
{
x: "116.39564504", #当前城市中心点经度
y: "39.92998578" #当前城市中心点纬度
}
},
status: 0 #结果状态返回码
}

淘宝API

http://ip.taobao.com/service/getIpInfo.php?ip=

返回结果示例:

     {"ip": "112.5.212.90",
"country": "中国",
"area": "",
"region": "福建",
"city": "福州",
"county": "XX",
"isp": "移动",
"country_id": "CN",
"area_id": "",
"region_id": "350000",
"city_id": "350100",
"county_id": "xx",
"isp_id": "100025"
}
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import requests
from bs4 import BeautifulSoup class Fill_Own_Ip(object): def __init__(self):
self.getIpInfoUrl_TaoBao = 'http://ip.taobao.com/service/getIpInfo.php?ip='
self.getIpInfoUrl_138 = 'http://ip138.com/ips138.asp' def sele_ip_taobao(self, ip):
url = self.getIpInfoUrl_TaoBao + str(ip)
try:
json_ret = requests.get(url, timeout=5).text
if json_ret:
ret = json.loads(json_ret)
ip_loc = ret['data']['country'] + ret['data']['region'] + ret['data']['city'] + ret['data']['isp']
else:
ip_loc = ''
except Exception as e:
ip_loc = ''
return ip_loc def sele_ip_138(self,ip):
kw2 = {'ip': ip}
try:
r = requests.request('GET', self.getIpInfoUrl_138, params=kw2)
r.encoding = 'gbk'
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
soup = soup.ul
ip_loc = soup.contents[2].string[6:].split(' ')[0] + soup.contents[0].string[5:]
except:
ip_loc = ''
return ip_loc if __name__ == '__main__': obj = Fill_Own_Ip()
ret_taobao = obj.sele_ip_taobao('112.5.212.90')
ret_138 = obj.sele_ip_138('112.5.212.90')
print 'ret_taobao',ret_taobao
print 'ret_138',ret_138

Python代码

国外的一个查询API

http://ip-api.com/json/  # 国际化英文显示

http://ip-api.com/json/?lang=zh-CN  # 中文显示

http://ip-api.com/json/115.191.200.34?lang=zh-CN  # 查询某个ip的信息

返回结果示例:

{
"accuracy": 50,
"as": "AS9808 Guangdong Mobile Communication Co.Ltd.",
"city": "Xiamen",
"country": "China",
"countryCode": "CN",
"isp": "China Mobile Guangdong",
"lat": 24.478,
"lon": 118.019,
"mobile": true,
"org": "China Mobile Guangdong",
"proxy": false,
"query": "xxx.xxx.xxx.xxx",
"region": "FJ",
"regionName": "Fujian",
"status": "success",
"timezone": "Asia/Shanghai", "zip": ""
}

126 IP API

http://ip.ws.126.net/ipquery?ip=

返回结果示例:

var lo="北京市", lc="朝阳区"; var localAddress={city:"朝阳区", province:"北京市"};