如果我有一组坐标,我怎样才能得到国家,只有请求库?
If I have a set of coordinates, how can I get the country, only with the Requests library?
我需要获取一组坐标来自的国家/地区:
示例:
coords=[41.902782, 12.496366.]
output:
Italy
我知道这可以通过使用其他库来实现,但我需要知道是否有办法只使用 requests 库。(json 也可用)
谢谢
正如@Razdi 所说,您将需要一个 API 获取您的坐标和 returns 一个位置。
将 Requests 库想象成浏览器 URL 路径。它所能做的就是获取网站地址。但是,如果地址是正确的,并且需要某些参数,那么您可以访问值:
>>> import requests
>>> url = 'https://maps.googleapis.com/maps/api/geocode/json'
>>> params = {'sensor': 'false', 'address': 'Mountain View, CA'}
>>> r = requests.get(url, params=params)
>>> results = r.json()['results']
>>> location = results[0]['geometry']['location']
>>> location['lat'], location['lng']
你想要的是这样的:
import geocoder
g = geocoder.google([45.15, -75.14], method='reverse')
但是你不能使用这个包...所以你需要更详细一点:
导入请求
def example():
# grab some lat/long coords from wherever. For this example,
# I just opened a javascript console in the browser and ran:
#
# navigator.geolocation.getCurrentPosition(function(p) {
# console.log(p);
# })
#
latitude = 35.1330343
longitude = -90.0625056
# Did the geocoding request comes from a device with a
# location sensor? Must be either true or false.
sensor = 'true'
# Hit Google's reverse geocoder directly
# NOTE: I *think* their terms state that you're supposed to
# use google maps if you use their api for anything.
base = "http://maps.googleapis.com/maps/api/geocode/json?"
params = "latlng={lat},{lon}&sensor={sen}".format(
lat=latitude,
lon=longitude,
sen=sensor
)
url = "{base}{params}".format(base=base, params=params)
response = requests.get(url)
return response.json()['results'][0]['formatted_address']
我需要获取一组坐标来自的国家/地区: 示例:
coords=[41.902782, 12.496366.]
output:
Italy
我知道这可以通过使用其他库来实现,但我需要知道是否有办法只使用 requests 库。(json 也可用) 谢谢
正如@Razdi 所说,您将需要一个 API 获取您的坐标和 returns 一个位置。
将 Requests 库想象成浏览器 URL 路径。它所能做的就是获取网站地址。但是,如果地址是正确的,并且需要某些参数,那么您可以访问值:
>>> import requests
>>> url = 'https://maps.googleapis.com/maps/api/geocode/json'
>>> params = {'sensor': 'false', 'address': 'Mountain View, CA'}
>>> r = requests.get(url, params=params)
>>> results = r.json()['results']
>>> location = results[0]['geometry']['location']
>>> location['lat'], location['lng']
你想要的是这样的:
import geocoder
g = geocoder.google([45.15, -75.14], method='reverse')
但是你不能使用这个包...所以你需要更详细一点:
导入请求
def example():
# grab some lat/long coords from wherever. For this example,
# I just opened a javascript console in the browser and ran:
#
# navigator.geolocation.getCurrentPosition(function(p) {
# console.log(p);
# })
#
latitude = 35.1330343
longitude = -90.0625056
# Did the geocoding request comes from a device with a
# location sensor? Must be either true or false.
sensor = 'true'
# Hit Google's reverse geocoder directly
# NOTE: I *think* their terms state that you're supposed to
# use google maps if you use their api for anything.
base = "http://maps.googleapis.com/maps/api/geocode/json?"
params = "latlng={lat},{lon}&sensor={sen}".format(
lat=latitude,
lon=longitude,
sen=sensor
)
url = "{base}{params}".format(base=base, params=params)
response = requests.get(url)
return response.json()['results'][0]['formatted_address']