Python: 使用IP2Location 地理数据库解析IP所在的地理位置

目录

一、注册&下载数据库

二、使用 Python 查询IP 的地理位置

2.1、下载 IP2Location 模块

2.2、解压 & install IP2Location

2.3、代码演示


一、注册&下载数据库

目前如果要使用 IP2Location 提供的数据库,需要先注册,然后才让下载,主页位置:免费IP地理位置数据库

注册、登录后,打开数据库下载页面:

https://lite.ip2location.com/database-download

然后就可以看到5个数据库,每个数据库又提供了IPV4和IPV6版本:

DB1.LITE IP-COUNTRY	 	 	
DB3.LITE IP-COUNTRY-REGION-CITY	 	 	
DB5.LITE IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE	 	 	
DB9.LITE IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE	 	 	
DB11.LITE IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE	 	 	

每个数据库提供的地理位置的完善程度不一样,我选择的 DB5,可以提供国家、省份、城市、经度和纬度五个信息。

二、使用 Python 查询IP 的地理位置

官网给了一个使用Python 查询 IP 地理位置的例子。

首先需要安装 IP2Location 的模块:

2.1、下载 IP2Location 模块

https://github.com/chrislim2888/IP2Location-Python/archive/master.zip

2.2、解压 & install IP2Location

unzip IP2Location-Python-master.zip
cd IP2Location-Python-master
python setup.py install

2.3、代码演示

安装完成后,就可以使用了。

# https://www.ip2location.com/development-libraries/ip2location/python

import IP2Location, os

'''
    Cache the database into memory to accelerate lookup speed.
    WARNING: Please make sure your system have sufficient RAM to use this feature.
'''
# database = IP2Location.IP2Location(os.path.join("data", "IPV6-COUNTRY.BIN"), "SHARED_MEMORY")
database = IP2Location.IP2Location(os.path.join("data", "IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-SAMPLE.BIN"))
rec = database.get_all("19.5.10.1")

print rec.country_short
print rec.country_long
print rec.region
print rec.city
print rec.isp
print rec.latitude
print rec.longitude
print rec.domain
print rec.zipcode
print rec.timezone
print rec.netspeed
print rec.idd_code
print rec.area_code
print rec.weather_code
print rec.weather_name
print rec.mcc
print rec.mnc
print rec.mobile_brand
print rec.elevation
print rec.usage_type
print rec.address_type
print rec.category

通过DB5的IPV4版数据库,可以查到如下信息:

rec.country_short US
rec.country_long  United States of America
rec.region        Michigan
rec.city          Dearborn
rec.isp           This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.latitude      42.311520
rec.longitude     -83.191322
rec.domain        This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.zipcode       This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.timezone      This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.netspeed      This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.idd_code      This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.area_code     This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.weather_code  This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.weather_name  This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.mcc           This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.mnc           This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.mobile_brand  This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.elevation     This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.usage_type    This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.address_type  This parameter is unavailable in selected .BIN data file. Please upgrade data file.
rec.category      This parameter is unavailable in selected .BIN data file. Please upgrade data file.

除了 Python 版本之外,官网还提供了例如C、PHP、Go等语言查询的样例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值