查询IP所在地区

使用Python查询IP所在地区

直接上代码

import json
import re

import requests


def search_sp0_baidu_com(ip):
    """http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=123.123.123.123&co=&resource_id=6006&oe=utf8"""
    url = f'http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&co=&resource_id=6006&oe=utf8'
    try:
        response = requests.get(url, timeout=5)
        json_obj = response.json()
        # json_obj数据
        """{'status': '0', 't': '', 'set_cache_time': '', 'data': [{'location': '北京市北京市 联通', 'titlecont': 
        'IP地址查询', 'origip': '123.123.123.123', 'origipquery': '123.123.123.123', 'showlamp': '1', 'showLikeShare': 1, 
        'shareImage': 1, 'ExtendedLocation': '', 'OriginQuery': '123.123.123.123', 'tplt': 'ip', 'resourceid': 
        '6006', 'fetchkey': '123.123.123.123', 'appinfo': '', 'role_id': 0, 'disp_type': 0}]} """
        ip_address = json_obj['data'][0]["location"]
    except Exception as e:
        return None
    else:
        return ip_address


def search_ip_api_com(ip):
    """http://ip-api.com/json/123.123.123.123?lang=zh-CN"""
    url = f'http://ip-api.com/json/{ip}?lang=zh-CN'
    try:
        response = requests.get(url, timeout=5)
        json_obj = response.json()
        # json_obj数据
        """{'status': 'success', 'country': '中国', 'countryCode': 'CN', 'region': 'BJ', 'regionName': 
        '北京市', 'city': '朝阳', 'zip': '', 'lat': 39.9771, 'lon': 116.384, 'timezone': 'Asia/Shanghai', 'isp': 'China 
        Unicom Sichuan Province Network', 'org': '', 'as': 'AS4837 CHINA UNICOM China169 Backbone', 
        'query': '123.123.123.123'} """
        ip_address = f"{json_obj['regionName']}{json_obj['city']}"
    except Exception as e:
        return None
    else:
        return ip_address


def search_ip_ws_126_net(ip):
    """http://ip.ws.126.net/ipquery?ip=123.123.123.123"""
    url = f'http://ip.ws.126.net/ipquery?ip={ip}'
    try:
        response = requests.get(url, timeout=5)
        text = response.text
        # text数据
        """var lo="北京市", lc="北京市";var localAddress={city:"北京市", province:"北京市"}"""
        city = re.match(r".*city:\"([\u4e00-\u9fa5]+)\".*", text, re.S).group(1)
        province = re.match(r".*province:\"([\u4e00-\u9fa5]+)\".*", text, re.S).group(1)
        ip_address = f'{province}{city}'
    except Exception as e:
        return None
    else:
        return ip_address


def search_whois_pconline_com_cn(ip):
    """http://whois.pconline.com.cn/jsFunction.jsp?callback=jsShow&ip=123.123.123.123"""
    url = f'http://whois.pconline.com.cn/ipJson.jsp?ip={ip}'
    try:
        response = requests.get(url, timeout=5)
        text = response.text.encode('utf-8')
        # text数据
        """if(window.IPCallBack) {IPCallBack({"ip":"123.123.123.123","pro":"北京市","proCode":"110000",
        "city":"北京市","cityCode":"110000","region":"顺义区","regionCode":"110113","addr":"北京市顺义区 联通","regionNames":"",
        "err":""});} """
        json_str = re.match(r".*?{IPCallBack\((.*)\);}.*", text, re.S).group(1)
        json_obj = json.loads(json_str)
        ip_address = f"{json_obj['pro']} {json_obj['city']} {json_obj['region']}"
    except Exception as e:
        return None
    else:
        return ip_address


def search_ip_address(ip):
    ip_address = search_sp0_baidu_com(ip)
    if ip_address is None:
        ip_address = search_ip_api_com(ip)
    if ip_address is None:
        ip_address = search_ip_ws_126_net(ip)
    if ip_address is None:
        ip_address = search_whois_pconline_com_cn(ip)
    return ip_address


if __name__ == '__main__':
    print(search_ip_address('123.123.123.123'))

希望大佬能多多指教

调用API

  1. http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=123.123.123.123&co=&resource_id=6006&oe=utf8这个是百度的 但是最好用的还是去百度地图的API
  2. http://ip-api.com/json/123.123.123.123?lang=zh-CN
  3. http://ip.ws.126.net/ipquery?ip=123.123.123.123
  4. http://whois.pconline.com.cn/jsFunction.jsp?callback=jsShow&ip=123.123.123.123

如果大家有更多的接口希望能评论分享

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值