购物国外网站的建立,低代码无代码开发平台,wordpress 更换谷歌,怎么样开网店卖东西python处理IP对应城市省份
IP地理地址库geoip2用法
数据包下载
数据包下载地址#xff08;需要注册#xff09; https://www.maxmind.com/en/accounts/258630/geoip/downloads 考虑到注册麻烦#xff0c;可以到下面这个github的链接去直接下载 https://github.com/Hackl0…python处理IP对应城市省份
IP地理地址库geoip2用法
数据包下载
数据包下载地址需要注册 https://www.maxmind.com/en/accounts/258630/geoip/downloads 考虑到注册麻烦可以到下面这个github的链接去直接下载 https://github.com/Hackl0us/GeoIP2-CN.git
代码
import os
import pprint
import geoip2.databasedef query_ip_location(ip_address, localeszh-CN):查询IP地址的地理位置信息:param ip_address: IP地址:param locales: 国际化如果locales不指定的话会默认输出英文我这里指定了中文:return: 返回IP信息#db_path os.path.dirname(__file__) /Country.mmdbdb_path rE:\ghm\加速器\GeoLite2-City.mmdbif not os.path.exists(db_path):raise FileNotFoundError(GeoLite2-City数据库不存在)try:with geoip2.database.Reader(db_path, locales[locales]) as reader:response reader.city(ip_address)location_info {country_name: response.country.name, # 国家名称中文显示country_iso_code: response.country.iso_code, # 国家ISO代码province_name: response.subdivisions.most_specific.name, # 省份名称中文显示province_iso_code: response.subdivisions.most_specific.iso_code, # 省份ISO代码city_name: response.city.name, # 城市名称中文显示postal_code: response.postal.code, # 邮政编码latitude: response.location.latitude, # 纬度longitude: response.location.longitude, # 经度time_zone: response.location.time_zone, # 时区continent_name: response.continent.name, # 大陆名称中文显示continent_code: response.continent.code # 大陆代码}return location_infoexcept Exception as e:print(f查询IP地址时发生错误: {e})return {}if __name__ __main__:# 使用示例ip 113.68.255.182ip_info query_ip_location(ip)pprint.pprint(ip_info[province_name]ip_info[city_name])展示结果 这是展示的详细数据