#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import sys import json url1 = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJkR44DpzPLKgRL-ht1vIiGPg&key=[PutHereYourGoogleAPIKey]' # Auckland #url1 = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJvXM_AUlp0TgRjUu7_XSI148&key=[PutHereYourGoogleAPIKey]' # Kabul response2 = requests.get(url = url1) # We take the GET response_data2 = response2.json() # We take the JSON long_field = {} # define long_field (will be used just for the first loop) # Here we put the first loop that works if response_data2['status'] == 'OK': # if the status is 'OK' GoogleName = response_data2['result']['name'] # grab the name for e in response_data2['result']['address_components']: # parse all the address_components for t in e['types']: # parse all the types long_field[t] = e['long_name'] # put all long_name in an array GoogleStreet_Number = long_field.get("street_number", None) # grab the street_number GoogleStreet = long_field.get("route", None) # grab the route GooglePostal_Code = long_field.get("postal_code", None) # grab the postal_code GoogleCity = long_field.get("locality", None) # grab the locality GoogleCountry = long_field.get("country", None) # grab the country print ("With first loop: ",GoogleName,'-',GoogleStreet_Number,'-',GoogleStreet,'-',GooglePostal_Code,'-',GoogleCity,'-',GoogleCountry) # Here we put the second loop that doesn't work # It will give you all None because there are the elif if response_data2['status'] == 'OK': GoogleName = response_data2['result']['name'] for types in response_data2['result']['address_components']: field = types.get('types', []) if 'street_number' in field: GoogleStreet_Number = types['long_name'] elif 'street_number' not in field: GoogleStreet_Number = None if 'route' in field: GoogleStreet = types['long_name'] elif 'route' not in field: GoogleStreet = None if 'postal_code' in field: GooglePostal_Code = types['long_name'] elif 'postal_code' not in field: GooglePostal_Code = None if 'locality' in field: GoogleCity = types['long_name'] elif 'locality' not in field: GoogleCity = None if 'country' in field: GoogleCountry = types['long_name'] elif 'country' not in field: GoogleCountry = None print ("With second loop:",GoogleName,'-',GoogleStreet_Number,'-',GoogleStreet,'-',GooglePostal_Code,'-',GoogleCity,'-',GoogleCountry) # Here we put the third loop, which is exactly like the previous one # but this time it will work becaue we deleted the elif if response_data2['status'] == 'OK': GoogleName = response_data2['result']['name'] for types in response_data2['result']['address_components']: field = types.get('types', []) if 'street_number' in field: GoogleStreet_Number = types['long_name'] #elif 'street_number' not in field: # GoogleStreet_Number = None if 'route' in field: GoogleStreet = types['long_name'] #elif 'route' not in field: # GoogleStreet = None if 'postal_code' in field: GooglePostal_Code = types['long_name'] #elif 'postal_code' not in field: # GooglePostal_Code = None if 'locality' in field: GoogleCity = types['long_name'] #elif 'locality' not in field: # GoogleCity = None if 'country' in field: GoogleCountry = types['long_name'] #elif 'country' not in field: # GoogleCountry = None print ("With third loop: ",GoogleName,'-',GoogleStreet_Number,'-',GoogleStreet,'-',GooglePostal_Code,'-',GoogleCity,'-',GoogleCountry)