哪个API、包或库最容易找到IP地址的位置?

2024-04-25 00:09:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的设想。我希望能够用python编写一个脚本,当输入一个IP地址时,它在API上查找该IP地址,并返回结果。谁能告诉我一个很好的工作查找API代码。例如,这是hostip:

import urllib

response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()

print(response)

但是hostIP的数据库很小,不能返回许多位置。在


Tags: 代码importinfo脚本apihttpgetresponse
2条回答

我在freegeoip.net上取得了很大的成功。它们提供一个公共(免费)API,或者您可以在自己的服务器上运行它。在

简单代码示例:

import urllib
response = urllib.urlopen('http://freegeoip.net/json/1.2.3.4').read()
print(response)

你可以看看userinfo.io。在

发出GET http://api.userinfo.io/userinfos将返回您所要求的所有信息。您也可以在参数中指定IP地址:GET http://api.userinfo.io/userinfos?ip_address=888.888.888.888。在

反应似乎是这样的:

{
  request_date: "2014-09-18T04:11:25.154Z",
  ip_address: "192.77.237.95",
  position: {
    latitude: 37.7758,
    longitude: -122.4128,
    accuracy: 3   // This is the accuracy radius, in kilometers
  },
  continent: {
    name: "North America",
    code: "NA",
  },
  country: {
    name: "United States",
    code: "US",
  },
  city: {
    name: "San Francisco",
    code: "94103"
  }
}

相关问题 更多 >