免费ip地址查询

  • 文章
  • 作者:Hubery
  • 发布时间:2020-03-25
  • 阅读数:2486
  • 分类:python
  • 标签: Django python 爬虫

原因:

最近看博客访问记录,记录ip地址记录很多都是空,觉得不正常,于是看下调用的淘宝ip查询接口,发现直返回502。没办法,只能在找一些免费的ip查询接口。(因为只记录大概地址,来识别是否为真人访问)免费的的精确度就足够了

下面是找到的一些免费查询ip地址的网站:


既然依赖一个接口有挂掉的风险,那么我们可以把这几个集合在一起,其中一个挂掉后就使用另一个。


django中获取用户ip的方法:

def get_ip(request):
    """
    获取ip
    :param request:
    :return:
    """
    if 'HTTP_X_FORWARDED_FOR' in request.META:
        ip = request.META.get('HTTP_X_FORWARDED_FOR')
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

获取ip地址的完整代码:依赖requests库

import json
import requests

BASE_HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1'
}


def get_ip_address_from_taobao(ip):
    """
    获取ip地址
    :param ip:
    :return:
    """
    url = 'http://ip.taobao.com//service/getIpInfo.php?ip={}'.format(ip)
    headers = BASE_HEADERS
    try:
        res = requests.get(url, headers=headers, timeout=5)
        if res.status_code == 200:
            res_dict_data = json.loads(res.text).get('data', '')
            country = res_dict_data.get('country', '')
            region = res_dict_data.get('region', '')
            city = res_dict_data.get('city', '')
            isp = res_dict_data.get('isp', '')
            ip_address = '/'.join([country, region, city, isp])
            return ip_address
        else:
            return ''
    except Exception:
        return None


def get_ip_address_from_pconline(ip):

    url = 'http://whois.pconline.com.cn/ipJson.jsp?ip={}&json=true'.format(ip)
    headers = BASE_HEADERS
    try:
        res = requests.get(url, headers=headers, timeout=5)
        addr = res.json().get('addr', None)
        if addr:
            ip_address = '/'.join(addr.split(' '))
        else:
            ip_address = None
        return ip_address
    except Exception:
        return None


def get_ip_from_ip_api(ip):

    url = 'http://ip-api.com/json/{}?lang=zh-CN'.format(ip)
    headers = BASE_HEADERS
    try:
        res = requests.get(url, headers=headers, timeout=5)
        res_json = json.loads(res.json())
        print(res_json)
        if res_json.get('status') == 'success':
            country = res_json.get('country')
            city = res_json.get('city')
            regionName = res_json.get('regionName')
            ip_address = '/'.join([country, city, regionName])
            return ip_address
        return None
    except Exception:
        return None


def get_ip_from_alaip(ip):

    url = 'https://v1.alapi.cn/api/ip/?ip={}'.format(ip)
    headers = BASE_HEADERS
    try:
        res = requests.get(url, headers=headers, timeout=5)
        res_json = res.json()
        if res_json.get('code') == 200:

            ad_info = res_json.get('data').get('ad_info')
            nation = ad_info.get('nation')
            province = ad_info.get('province')
            city = ad_info.get('city')
            district = ad_info.get('district')
            ip_address = '/'.join([nation, province, city, district])
            return ip_address
        return None
    except Exception:
        return None


def get_ip_address(ip):
    get_ip_func_list = [get_ip_from_alaip, get_ip_from_ip_api, get_ip_address_from_pconline, get_ip_address_from_taobao]
    for get_ip_func in get_ip_func_list:
        ip_address = get_ip_func(ip)
        if ip_address:
            return ip_address
    return None


if __name__ == '__main__':

    ip = get_ip_address('127.0.0.1')
    print(ip)


评论列表
  法撒旦法 (2020-03-28 12:12): 回复
    fdsf asdfasdf fdfdasf sdfsd fsdf

新的评论