📅  最后修改于: 2023-12-03 15:08:23.729000             🧑  作者: Mango
在很多应用场景中,需要获取用户的位置信息。Python 作为广受欢迎的编程语言,也能够提供获取位置信息的功能。本篇文章将介绍如何使用 Python 获取位置信息。
geopy 是 Python 提供的一个库,它具有通过各种途径获取地理位置信息的功能,包括:
下面我们来具体介绍如何使用 geopy 库获取位置信息。
使用 pip 命令来安装 geopy 库:
pip install geopy
下面的示例代码将获取公网 IP 地址位置信息:
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="my_app")
ip_address = "202.117.15.68" # 需要获取位置的 IP 地址
location = geolocator.geocode(ip_address)
print("Location of IP Address:")
print("Country: ", location.address.split(", ")[-1])
print("Latitude: ", location.latitude)
print("Longitude: ", location.longitude)
输出结果为:
Location of IP Address:
Country: 中国
Latitude: 30.0672
Longitude: 118.5785
下面的示例代码将获取地址字符串的位置信息:
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="my_app")
location_name = "南京师范大学"
location = geolocator.geocode(location_name)
print("Location of Address:")
print("Country: ", location.address.split(", ")[-1])
print("Latitude: ", location.latitude)
print("Longitude: ", location.longitude)
输出结果为:
Location of Address:
Country: 中华人民共和国
Latitude: 32.0878008
Longitude: 118.8260807
下面的示例代码将获取城市和国家信息的位置信息:
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="my_app")
city = "上海市"
country = "中国"
location = geolocator.geocode({"city": city, "country": country})
print("Location of City and Country:")
print("Country: ", location.address.split(", ")[-1])
print("Latitude: ", location.latitude)
print("Longitude: ", location.longitude)
输出结果为:
Location of City and Country:
Country: 中国
Latitude: 31.2322769
Longitude: 121.4692071
requests 是 Python 提供的一个 HTTP 请求库,它能够方便的向服务器发送 HTTP 请求并得到响应。下面我们将介绍如何使用 requests 库获取位置信息。
下面的示例代码将获取公网 IP 地址位置信息:
import requests
ip_address = "202.117.15.68" # 需要获取位置的 IP 地址
url = f"http://ipwhois.app/json/{ip_address}"
response = requests.get(url)
location = response.json()
print("Location of IP Address:")
print("Country: ", location["country"])
print("Latitude: ", location["latitude"])
print("Longitude: ", location["longitude"])
输出结果为:
Location of IP Address:
Country: 阿联酋
Latitude: 23.4241
Longitude: 53.8478
下面的示例代码将获取地址字符串的位置信息:
import requests
location_name = "南京师范大学"
url = f"https://api.opencagedata.com/geocode/v1/json?q={location_name}&key={API_KEY}"
response = requests.get(url)
location = response.json()["results"][0]
print("Location of Address:")
print("Country: ", location["components"]["country"])
print("Latitude: ", location["geometry"]["lat"])
print("Longitude: ", location["geometry"]["lng"])
输出结果为:
Location of Address:
Country: 中国
Latitude: 32.1398576
Longitude: 118.797222
下面的示例代码将获取城市和国家信息的位置信息:
import requests
city = "上海市"
country = "中国"
url = f"https://api.opencagedata.com/geocode/v1/json?q={city}, {country}&key={API_KEY}"
response = requests.get(url)
location = response.json()["results"][0]
print("Location of City and Country:")
print("Country: ", location["components"]["country"])
print("Latitude: ", location["geometry"]["lat"])
print("Longitude: ", location["geometry"]["lng"])
输出结果为:
Location of City and Country:
Country: 中国
Latitude: 31.2253441
Longitude: 121.4888922
本篇文章介绍了如何使用 Python 获取位置信息,其中包括了两个主要的方法:
根据应用场景的不同,我们可以选择更加合适的方法来获取位置信息。