【发布时间】:2021-06-11 16:58:12
【问题描述】:
我目前有一列 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 地址获取纬度/经度(在几次请求后不会过期?)。
【问题讨论】:
-
我指的是这种类似类型的question with answers in stack overflow。我认为它会让你摆脱这个问题。
标签: python api get ip-address