我目前有一列IP地址,我想用相应的纬度/经度列来表示
我目前正在使用以下方法,但速度非常慢:
def getting_ip(row,latlong):
url = r'https://freegeoip.app/json/'+row
headers = {
'accept': "application/json",
'content-type': "application/json"
}
response = requests.request("GET", url, headers=headers)
respond = json.loads(response.text)
if respond[latlong]:
return respond[latlong]
else:
return None
df['Latitude'] = [getting_ip(i,'latitude') if i else None for i in df['IP']]
df['Longitude'] = [getting_ip(i,'longitude') if i else None for i in df['IP']]
在定义中使用.apply也不会节省我很多时间,我大部分时间都花在收回请求上。是否有一种免费、简单的方法可以从ip地址获取纬度/经度(几次请求后不会过期?)
您可以使用IP2Location Python库获取IP地址的纬度和经度。比如说,
上面的示例代码使用了IP2Location LITE DB5 database。您可以在此处注册并免费获取数据库:https://lite.ip2location.com/sign-up
为此,如果您更喜欢python,那么我发现以下软件包:
ip2geotools
ip2geotools它可以从IP地址提取纬度和经度
ip2geotools是一个简单的工具,用于从各种地理位置数据库获取给定IP地址的地理位置信息。这个包为几个地理定位数据库提供了一个API
一个例子如下:
Geoip2
还提供了另一个包geoip2,它与此包类似,但具有异步请求处理支持和单独的数据库搜索选项
如果需要的话,您可以查看他们的网站上的同步和异步web服务示例
IP2定位
由于这个API大部分是付费的,我试图找到一个免费版本,我发现这个名为Ip2location Python的库,它在一个月内为您提供30000次免费查找
相关问题 更多 >
编程相关推荐