I use the Python Client for Google Maps Services to get following data from google-maps:
{
'address_components':[
{
'long_name':'20',
'short_name':'20',
'types':[
'street_number'
]
},
{
'long_name':'Oberböhl',
'short_name':'Oberböhl',
'types':[
'route'
]
},
{
'long_name':'Ingelheim am Rhein',
'short_name':'Ingelheim am Rhein',
'types':[
'locality',
'political'
]
},
{
'long_name':'Mainz-Bingen',
'short_name':'Mainz-Bingen',
'types':[
'administrative_area_level_3',
'political'
]
},
{
'long_name':'Rheinland-Pfalz',
'short_name':'RP',
'types':[
'administrative_area_level_1',
'political'
]
},
{
'long_name':'Germany',
'short_name':'DE',
'types':[
'country',
'political'
]
},
{
'long_name':'55218',
'short_name':'55218',
'types':[
'postal_code'
]
}
],
'adr_address':'<span class="street-address">Oberböhl 20</span>, <span class="postal-code">55218</span> <span class="locality">Ingelheim am Rhein</span>, <span class="country-name">Germany</span>',
'formatted_address':'Oberböhl 20, 55218 Ingelheim am Rhein, Germany',
'formatted_phone_number':'06132 5099968',
'geometry':{
'location':{
'lat':49.9810156,
'lng':8.0739617
},
'viewport':{
'northeast':{
'lat':49.9823942302915,
'lng':8.075293780291501
},
'southwest':{
'lat':49.9796962697085,
'lng':8.072595819708498
}
}
},
'icon':'https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png',
'id':'d2b37ffe23fd5e76648a90df2987558b039fcdf7',
'international_phone_number':'+49 6132 5099968',
'name':'Esch Metalltechnik GmbH',
'place_id':'ChIJHaERGJ_svUcRRfqNoGXq3EU',
'plus_code':{
'compound_code':'X3JF+CH Ingelheim am Rhein, Germany',
'global_code':'8FXCX3JF+CH'
},
'reference':'ChIJHaERGJ_svUcRRfqNoGXq3EU',
'scope':'GOOGLE',
'types':[
'general_contractor',
'point_of_interest',
'establishment'
],
'url':'https://maps.google.com/?cid=5034156205699627589',
'utc_offset':60,
'vicinity':'Oberböhl 20, Ingelheim am Rhein',
'website':'http://www.esch-metalltechnik.de/'
}{
'long_name':'55218',
'short_name':'55218',
'types':[
'postal_code'
]
}
Now I want to extract certain variables, like the "street_number". I don't know which format this data is, so I worked with it like a dictionary:
try:
self.hausnr = place_result_2["address_components"][0]["long_name"]
except:
self.hausnr = "NA"
The problem is, that the index "0" isn't always the same position of the data I want, i varies. Is there a way to extract the data in another way? Perhaps I have to use a JSON-parser or something similar?
Thanks a lot.