【问题标题】:API Request within another API request (Same API) in PythonPython 中另一个 API 请求(相同 API)中的 API 请求
【发布时间】:2021-05-15 01:50:38
【问题描述】:

我目前制作了一个 python 程序,从 API 请求 JSON 数据。现在,虽然这个 JSON 实际上包含其他请求 Url 以从该对象获取额外数据,但事情就是这样。

import requests

import json

import sys

import os

import geojson



response = requests.get("http://api.gipod.vlaanderen.be/ws/v1/workassignment", params = {"CRS": "Lambert72"})

print(response.status_code)
text = json.dumps(response.json(),sort_keys=True, indent=4)
print(text)

f = open("text.json", "wt")
f.write(text)

print(os.getcwd())

来自请求的JSON,包括参数在内的其他请求URL在详细列中。

[
{
"gipodId": 103246,
"owner": "Eandis Leuven",
"description": ", , ZAVELSTRAAT: E Nieuw distributienet (1214m)",
"startDateTime": "2007-12-03T06:00:00",
"endDateTime": "2014-01-06T19:00:00",
"importantHindrance": false,
"coordinate": {
"coordinates": [
4.697028256276443,
50.896894135898485
],
"type": "Point",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
}
},
**"detail": http://api.gipod.vlaanderen.be/ws/v1/workassignment/103246?crs=4326,
"cities": ["Leuven"]**
}
],
"latestUpdate": "2016-11-16T11:32:39.253"
}

第一个请求只获取点(每个点都有特定的 id),而第二个请求获取“详细信息数据”,其中还包含多边形数据和多线数据。

获取网址: http://api.gipod.vlaanderen.be/ws/v1/workassignment/[id]

{ "comment" : null,
  "contactDetails" : { "city" : "Leuven",
      "country" : "België",
      "email" : null,
      "extraAddressInfo" : null,
      "firstName" : null,
      "lastName" : null,
      "number" : "58",
      "organisation" : "Eandis Leuven",
      "phoneNumber1" : "078/35.35.34",
      "phoneNumber2" : null,
      "postalCode" : "3012",
      "street" : "Aarschotsesteenweg"
    },
  "contractor" : null,
  "mainContractor" : null,
  "description" : ", , ZAVELSTRAAT: E Nieuw distributienet (1214m)",
  "diversions" : [
    {
    "gipodId": 1348152,
    "reference": "IOW-TERRAS-2013-01-Z",
    "description": "Horecaterras op parkeerstrook (Lierbaan 12)",
    "comment": null,
    "geometry": {
    "geometries": [
    {
        "coordinates": [[[3.212947654779088, 51.175784679668915],
        [3.2151308569159482, 51.17366647833133],
        [3.216112818368467,  51.17328051591839],
        [3.2186926906668876, 51.173044950954456],
        [3.2204789191276944, 51.173098278776514],
        [3.221602856602255,  51.173333934695286]]],
        "type": "MultiLineString",
        "crs": null
        }
        ],
        "type": "GeometryCollection",
        "crs": {
        "type": "name",
        "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
        }
    },
    "periods": [{"startDateTime": "2013-04-09T00:00:00","endDateTime": "2013-10-31T00:00:00"}],
    "recurrencePattern": null,
    "latestUpdate": "2014-01-24T10:23:08.917",
    "streets": null,
    "diversionTypes": null,
    "diversionDirection": 
        {
            "type": 0,
            "description": "Beide"
        },
    "status": "Vergund",
    "contactDetails": {
        "organisation": "Café Real",
        "lastName": "Vets",
        "firstName": "Peggy",
        "phoneNumber1": null,
        "phoneNumber2": null,
        "email": "peggy.vets@skynet.be",
        "street": "Lierbaan",
        "number": "12",
        "postalCode": "2580",
        "city": "Putte",
        "country": "België",
        "extraAddressInfo": null
        }
   "url": null,
    }
    ],
  "endDateTime" : "2014-01-06T19:00:00",
  "gipodId" : 103246,
  "hindrance" : { "description" : null,
      "direction" : null,
      "effects" : [ "Fietsers hebben doorgang",
          "Handelaars bereikbaar",
          "Verminderde doorstroming in 1 richting",
          "Voetgangers op de rijweg",
          "Voetgangers hebben doorgang"
        ],
      "important" : false,
      "locations" : [ "Voetpad" ]
    },
  "latestUpdate" : "2013-06-18T03:43:28.17",
  "location" : { "cities" : [ "Leuven" ],
      "coordinate" : { "coordinates" : [ 4.697028256276443,
              50.896894135898485
            ],
          "crs" : { "properties" : { "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" },
              "type" : "name"
            },
          "type" : "Point"
        },
      "geometry" : { "coordinates" : [ [ [ [ 4.699934331336474,
                    50.90431808607037
                  ],
                  [ 4.699948535632464,
                    50.90431829749237
                  ],
                  [ 4.699938837004092,
                    50.90458139231922
                  ],
                  [ 4.6999246328435396,
                    50.90458118062111
                  ],
                  [ 4.699934331336474,
                    50.90431808607037
                  ]
                ] ]
            ],
          "crs" : { "properties" : { "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" },
              "type" : "name"
            },
          "type" : "MultiPolygon"
        }
    },
  "owner" : "Eandis Leuven",
  "reference" : "171577",
  "startDateTime" : "2007-12-03T06:00:00",
  "state" : "In uitvoering",
  "type" : "Werken aan nutsleiding",
  "url" : "http://www.eandis.be"
}

现在是这样,对于我从第一个 API 请求中获得的每个对象,都必须重复此请求。这可以是一百多个对象。所以逻辑表明这必须在一个循环中发生,尽管如何开始有点..麻烦。

【问题讨论】:

    标签: python-3.x api


    【解决方案1】:

    在这种情况下,您可以使用函数。

    您的第一个函数可以简单地获取点列表。您的第二个函数可以简单地获取详细信息的数据。

    
    def fetch_details(url: str):
          """ Makes request call to get the data of detail """
            response = requests.get(url)
            # any other processe
    
    
    def fetch_points(url: str):
           
           response = requests.get(url)
           for obj in response.json():
                  fetch_details(obj.get("detail"))
    
    api_url = "api.gipod.vlaanderen.be/ws/v1/workassignment"
    fetch_points(api_url)
    

    【讨论】:

    • 所以为了实现我所要求的所有点,我必须这样做对吗?:apiurl = api.gipod.vlaanderen.be/ws/v1/workassignment fetch_details(feth_points(apiurl))
    • 我认为只要这样做:api_url = api.gipod.vlaanderen.be/ws/v1/workassignment fetch_points(api_url) 就足够了
    • 谢谢,我可以确认这个工作。打印返回。现在我再进一步。稍后我会问一个或两个不同的问题。我从这里走得更远,因为让我说当我在 api_url 中包含参数时我必须修改它。我想我已经知道答案了,只是添加了一些额外的字符串。这个答案是正确的。再次感谢您。
    • 不用担心。我期待着你的问题。继续编码,继续探索!
    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 2022-09-23
    • 2019-07-29
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多