Google Places API 是一个强大的工具,允许开发者通过地点名称、地址或其他标识符来搜索和获取有关地点的信息。返回的数据包括地点的详细信息,如名称、地址、地理位置(经纬度)、评分、营业时间等。
邮政编码 是一个用于邮件投递的区域代码,通常由数字组成,用于标识特定的地理区域或邮政服务区域。
问题:Google Places API返回的邮政编码不准确或为空。
原因:
import requests
def get_postal_code(place_id):
api_key = "YOUR_GOOGLE_PLACES_API_KEY"
url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&fields=address_components&key={api_key}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
for component in data['result']['address_components']:
if 'postal_code' in component['types']:
return component['long_name']
return "Postal code not found"
else:
return f"Error: {response.status_code}"
# Example usage
place_id = "ChIJN1t_tDeuEmsRUsoyG83frY4" # Example place ID for Google Sydney
postal_code = get_postal_code(place_id)
print(f"Postal Code: {postal_code}")
YOUR_GOOGLE_PLACES_API_KEY
为你的实际API密钥。通过以上方法,你可以有效地获取和使用Google Places API返回的邮政编码信息。
领取专属 10元无门槛券
手把手带您无忧上云