我正在努力实现这一目标:
使用postcodes.io获取每个邮政编码的纬度和经度 并将其呈现在模板中每个商店位置旁边
我在json
文件中添加了一系列邮政编码:
[
{
"name": "St_Albans",
"postcode": "AL1 2RJ"
},
{
"name": "Hatfield",
"postcode": "AL9 5JP"
},
{
"name": "Worthing",
"postcode": "BN14 9GB"
},
{
"name": "Rustington",
"postcode": "BN16 3RT"
},
{
"name": "Eastbourne",
"postcode": "BN23 6QD"
}, ...
依此类推...
我需要检查postcodes.io
[0]这些邮政编码的纬度和经度,并将这些坐标显示在json
结果的旁边。
我的问题不是关于如何渲染它们,而是关于如何将我的json邮政编码张贴在他们的网站上,以及如何获取每个人的各自的纬度和经度。
我在纯Python上执行此操作,有什么想法吗?
答案 0 :(得分:2)
您可以尝试使用requests。尝试这样:
resp = requests.get('https://api.postcodes.io/postcodes/BN14 9GB')
resp.json()
或
data = {
"postcodes" : ["PR3 0SG", "M45 6GN", "EX165BL"]
}
resp = requests.post('https://api.postcodes.io/postcodes/', data=data)
resp.json()
将这些与您的json文件集成:
json_data = open('path_to/postcodes.json').read()
for i in json_data:
post_code = i.get('postcode')
resp = requests.get('https://api.postcodes.io/postcodes/{}'.format(postcode))
i.update({'latitude': resp.json().get('latitude'), 'longitude': resp.json().get('longitude')})
print(json_data)