使用Python获取街道地址的美国邮政编码

6

如何使用Python以最有效的方式检索街道地址的美国邮政编码?这是可能的吗?

最好使用本地数据库而不是某种远程API调用。

提前感谢任何能够提供帮助的人。

2个回答

4
也许可以从这里开始: 邮政编码数据库项目

googlemaps - Python中的Google Maps和本地搜索API

GoogleMaps.geocode(query, sensor='false', oe='utf8', ll='', spn='', gl='')

给定一个字符串地址查询,返回该位置的信息字典,包括其纬度和经度。 有趣的部分:
>>> gmaps = GoogleMaps(api_key)
>>> address = '350 Fifth Avenue New York, NY'
>>> result = gmaps.geocode(address)
>>> placemark = result['Placemark'][0]
>>> lng, lat = placemark['Point']['coordinates'][0:2]
# Note these are backwards from usual
>>> print lat, lng
40.6721118 -73.9838823
>>> details = placemark['AddressDetails']['Country']['AdministrativeArea']
>>> street = details['Locality']['Thoroughfare']['ThoroughfareName']
>>> city = details['Locality']['LocalityName']
>>> state = details['AdministrativeAreaName']
>>> zipcode = details['Locality']['PostalCode']['PostalCodeNumber']
>>> print ', '.join((street, city, state, zipcode))
350 5th Ave, Brooklyn, NY, 11215

0
更可靠的方法是使用一个强大的地址验证服务,该服务使用最新的USPS地址数据。完全透明地说,我为SmartyStreets工作,这是一家提供此类服务的地址验证软件公司。以下是一个简单的示例,说明如何使用该服务:

https://github.com/smartystreets/LiveAddressSamples/blob/master/python/street-address.py

从示例中可以看出,该服务提供了完整的9位数字邮政编码,以及所有其他地址组件以及有关地址的一些元数据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接