工具推荐-根据IP地址精确定位经纬度(永久免费)


   今天小张由于业务需求,需要根据用户的访问ip精确定位用户的国家、城市、及经纬度等信息,从网上进行搜索,发现不少的网站,但几乎没有完全符合的,有个别符合的还需要花钱,大家也知道,现在是信息共享的时代,难道真没有免费的API接口可调用吗?

继续搜索,终于功夫 不负有心人,找到了,就是它,就是它 。先不急,先看代码吧。

import requests,traceback,logging,random
from retry import retry


user_agent_list = [
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 "
    "(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
    "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 "
    "(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 "
    "(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
    "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 "
    "(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
    "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 "
    "(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 "
    "(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
    "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 "
    "(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
    "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 "
    "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
    "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
    "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
    "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
    "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
]
headers = {
'Connection': 'close',
"User-Agent": random.choice(user_agent_list)
}
#根据ip查询国家
@retry(Exception, tries=2, delay=6)
def ip_map(ip):
        url=f"http://ip-api.com/json/{ip}?fields=61439&lang=zh-CN"  
        response=requests.get(url=url,headers=headers,timeout=5).json()
        print(response)


if __name__ == '__main__':
    try:
        ip_map("212.32.35.64")
    except:
        val = traceback.format_exc()
        logging.error("错误位置信息为>>>%s" % val)

  结果如下:

{
    'status': 'success',
    'country': '英国',
    'countryCode': 'GB',
    'region': 'WLS',
    'regionName': 'Wales',
    'city': 'Radyr',
    'zip': 'CF15 8EN',
    'lat': 51.5318,
    'lon': -3.25879,
    'timezone': 'Europe/London',
    'isp': 'MISTRAL',
    'org': 'Frontier  Limited',
    'as': 'AS8897 KCOM Group Public Limited Company',
    'query': '212.32.35.64'
}

具体代码不过多解释,so_easy,先看下他们官网的介绍

小张来给大家翻译下(其实就是用的谷歌浏览器的翻译功能^_^):

怎么样是不是很强大。

具体官方API文档,可扫描右上角二维码,关注后回复IP,获取详细文档。

作者:小张学Python
本文链接: https://mp.weixin.qq.com/s?__biz=MzUzODk1MDY4NA==&mid=2247484003&idx=1&sn=d0e78b1b89a8899cbaefea3a3e2653ee&chksm=faceaf10cdb9260671c890eb37c92937235353d80a966a98e21c42290fcf5aa3a0b3261ca71b&token=1495817177&lang=zh_CN#rd
转载请注明来源!!  

创作不易,期待你的打赏!!