我需要获取一组坐标来自的国家: 例如:
coords=[41.902782, 12.496366.]
output:
Italy
我知道这可以通过使用其他库来实现,但是我需要知道是否只有通过 requests库才能做到这一点。( json 是也可用) 谢谢。
答案 0 :(得分:0)
就像@Razdi所说的那样,您将需要一个获取坐标并返回位置的API。
将请求库视为浏览器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']