Python神库分享之geoip2 IP定位库

先安装这两个

pip install python-geoip-geolite2 -i https://pypi.douban.com/simple

pip install geoip2

然后下载资源

Python神库分享之geoip2 IP定位库

搜一搜GeoLite2-Country.mmdb第一个链接就有

Python神库分享之geoip2 IP定位库

测试代码

import geoip2.database

reader = geoip2.database.Reader('./GeoLite2-City.mmdb')

response = reader.city('128.101.101.101')

response.country.iso_code

'US'

response.country.name

'United States'

response.country.names['zh-CN']

u'美国'

response.subdivisions.most_specific.name

'Minnesota'

response.subdivisions.most_specific.iso_code

'MN'

response.city.name

'Minneapolis'

response.postal.code

'55455'

response.location.latitude

44.9733

response.location.longitude

-93.2323

reader.close()

原文地址:https://www.cnblogs.com/cuiyubo/p/8318100.html