Python 在Django上使用postcodes.io API

Python 在Django上使用postcodes.io API,python,json,django,Python,Json,Django,我正在努力做到这一点: 使用postcodes.io获取每个邮政编码的纬度和经度 并在模板中的每个存储位置旁边渲染它们 我将一系列邮政编码放入json文件中: [ { "name": "St_Albans", "postcode": "AL1 2RJ" }, { "name": "Hatfield", "postcode": "AL9 5JP" }, { "name": "Worthing", "postcode": "BN14 9GB" }, { "name": "

我正在努力做到这一点:

使用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。-

您可以尝试使用。试着这样做:

resp = requests.get('https://api.postcodes.io/postcodes/BN14 9GB')
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)

这不是真的关于Django那么。。。你需要做的是用你的邮政编码向网站发帖请求?请看一看,这将使对指定的端点执行POST请求和检索数据变得容易。如果您只需要使用python stdlib,那么您需要查看
urlib.request
stuff。
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)