1. 程式人生 > >快速獲取IP地址的各種方法

快速獲取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

引數

返回結果示例:

{  
    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:"北京市"};