使用Python获取设备的物理位置?

4

有没有一种使用Python获取计算机物理位置的方法,最好不使用API或使用免费API?我已经搜索了一下,但是我发现唯一的免费API非常不准确。我只需要它相对准确一些,因为我要用它来获取本地天气。


  1. 你尝试了什么?
  2. 你想要多精确?
- mfsiega
@mfrankli 我搜索了“IP地理位置API”,只找到了IP2location.com。我住在一个大城市,他们网站上我的IP的示例偏差很大。虽然它是用于查找本地天气的,所以大约50英里还可以接受,但我发现的这个几乎错了一个州。 - tkbx
你可能想要更新你的问题以反映这些事情(尤其是后者)。 - mfsiega
基于IP的位置检测并不总是非常准确。当你通过其他ISP或从其他地点(朋友家等)连接时,应该检查你尝试的工具,并在每个位置上使用多个工具来检查你报告的位置。不要太快责怪工具本身。 - Silas Ray
7个回答

5

4

有没有一种方法可以使用Python获取计算机的物理位置,最好不需要API或免费API

是的。使用IPinfo.io,您可以免费进行50000次请求/月。如果您想获取天气数据,可以通过进行IP地理位置查找来使用提供的regioncity值。您可以通过使用请求库与API通信或使用IPinfo Python库来使用IPinfo API

方法1:使用requests与API通信

import requests

# here I have generated this IP address randomly
ip_address = "19.145.189.86"

# get your token from your IPinfo account dashboard
token = ""

# using f-string here. Use any method to create the URL
url = f"https://ipinfo.io/{ip_address}?token={token}"

# json response after the IP lookup
response = requests.get(url).json()

响应字典的外观将如下所示:
{
  "ip": "19.145.189.86",
  "city": "Dearborn",
  "region": "Michigan",
  "country": "US",
  "loc": "42.2399,-83.1508",
  "postal": "48121",
  "timezone": "America/Detroit"
}

为了获得最准确的天气预报,请使用cityregion值。因此,从response字典中查找cityregion的值。
>>> response['city']
"Dearborn"

>>> response['region']
"Dearborn"

方法2:使用Python库

IPinfo Python库可以使这个过程更加容易。使用pip进行安装。IPinfo Python文档

# install with `pip install ipinfo`
import ipinfo

# initialize handler with access token
access_token = "insert_your_token_here"
handler = ipinfo.getHandler(access_token)

ip_address = '19.145.189.86'
details = handler.getDetails(ip_address)

现在你可以从details变量中找到你需要的个人信息。

>>> details.city
'Dearborn'

>>> details.region
'Michigan'

4
import urllib2
import json

# Automatically geolocate the connecting IP
f = urllib2.urlopen('http://freegeoip.net/json/')
json_string = f.read()
f.close()
location = json.loads(json_string)
print(location)
location_city = location['city']
location_state = location['region_name']
location_country = location['country_name']
location_zip = location['zipcode']

请发送HTTP GET请求至: freegeoip.net/{format}/{ip_or_hostname},以接收JSON输出,Python可以解析。
以下是我得到的JSON键名,应该足以满足您的需求: - IP - 国家代码 - 国家名称 - 地区代码 - 地区名称 - 城市 - 邮政编码 - 纬度 - 经度 - 大都市代码 - 区域代码

1
此 API 端点已被弃用并已关闭。 - Hugo

2

爬虫并不理想,使用Web API要好得多,因为它不太容易受到解析变化的影响。请参见下面的答案。 - Jason Parham
@JasonParham 目前 http://freegeoip.net/ 无法使用,爬取或使用 Web API 比使用 GeoIP 数据库更糟糕,你可以离线下载并且不必担心这些服务宕机或从服务器下载数据所需的时间。 - jamylak

0

如果您知道设备的公共IP,则可以使用freegeip来获取。以下是Python 3.4.2代码,用于获取位置和时区。

>>> import requests
>>> ip = '141.70.111.66'
>>> url = 'http://freegeoip.net/json/'+ip
>>> r = requests.get(url)
>>> js = r.json()
>>> js['country_code']
'DE'
>>> js['country_name']
'Germany'
>>> js['time_zone']
'Europe/Berlin'
>>> js['city']
'Stuttgart'
>>> js.items()
dict_items([('latitude', 48.7667), ('ip', '141.70.111.66'), ('region_code', 'BW'), ('country_code', 'DE'), ('city', 'Stuttgart'), ('zip_code', '70173'), ('country_name', 'Germany'), ('longitude', 9.1833), ('region_name', 'Baden-Württemberg Region'), ('time_zone', 'Europe/Berlin'), ('metro_code', 0)])

1
此 API 端点已被弃用并已关闭。 - Hugo

0

1
Seba Ledezma,欢迎提供解决方案的链接,但请确保您的答案即使没有链接也很有用:添加链接周围的上下文,以便您的同行用户了解它是什么以及为什么在那里,在目标页面不可用的情况下,请引用您链接的页面上最相关的部分。只包含链接的答案可能会被删除。 - STA
目前你的回答不够清晰,请编辑并添加更多细节以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

我重新发现了另一个天气API,虽然我不太喜欢它(Weather Underground),但可以选择确定位置。如果我无法让类似于geoiptool的爬虫工作,可能会使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接