IP地理地址库geoip2用法
数据包下载地址(需要注册)
https://www.maxmind.com/en/accounts/258630/geoip/downloads
# pip insall geoip2
import geoip2.databasereader = geoip2.database.Reader('./GeoLite2-City.mmdb')ip = '101.43.150.169'
response = reader.city(ip) # 有多种语言,我们这里主要输出英文和中文print("你查询的IP的地理位置是:")
try:print("地区:{}({})".format(response.continent.names["es"], response.continent.names["zh-CN"]))print("国家:{}({}) ,简称:{}".format(response.country.name, response.country.names["zh-CN"], response.country.iso_code))print("洲/省:{}({})".format(response.subdivisions.most_specific.name,response.subdivisions.most_specific.names["zh-CN"]))print("城市:{}({})".format(response.city.name, response.city.names["zh-CN"]))print("经度:{},纬度{}".format(response.location.longitude, response.location.latitude))print("时区:{}".format(response.location.time_zone))print("邮编:{}".format(response.postal.code))
except:passaddress = ''
try:response = reader.city(ip)try:address += response.country.names["zh-CN"] + ' 'except:try:address += response.country.name + ' 'except:passtry:address += response.subdivisions.most_specific.names["zh-CN"] + ' 'except:try:address += response.subdivisions.most_specific.name + ' 'except:passtry:address += response.city.names["zh-CN"] + ' 'except:try:address += response.city.name + ' 'except:pass
except:address = '未成功解析地址'
print(address)