从Python中的地址提取房屋地址city和zip

时间:2014-08-28 14:27:09

标签: python api google-maps maps

我有可变地址字符串,例如:

1234 Maple Ave. Queens, NY 11011

这种格式因字符串而异,所以我可以使用简单的字符串解析/拆分。 有什么方法可以连接谷歌地图或其他api,它会查找地址并分别给我所有地址单位?

理想情况下,我应该能够做到这样的事情:

>> address = "1234 Maple Ave. Queens, NY 11011"
>> data = maps_api.extract_data(address)
>> print data.house_number
>> 1234 Maple Ave. 
>> print data.city
>> Queens
>> print data.state
>> NY
>> print data.zip
>> 11011

1 个答案:

答案 0 :(得分:2)

这似乎是googlemaps api的完美之选。

from googlemaps import GoogleMaps
gmaps = GoogleMaps(api_key)
address = "1234 Maple Ave. Queens, NY 11011"

result = gmaps.geocode(address)
placemark = result['Placemark'][0]
details = placemark['AddressDetails']['Country']['AdministrativeArea']

city  = details['Locality']['LocalityName']
state = details['AdministrativeAreaName']
zip   = details['Locality']['PostalCode']['PostalCodeNumber']

但是你需要一个api密钥。

备选方案似乎有pygeocoder