如何创建一个以 json 格式提取数据并将其存储在 csv 文件中的循环?

How to create a loop that extract data in json format and store it in a csv file?

我目前正在尝试创建一种算法来帮助我从具有以下参数 http://website/example/example/YYYY-MM-DD%20HH/1 的网站中提取数据。例如,如果我想提取 2022 年 2 月 3 日下午 3 点的数据,link 应该是 http://website/example/example/2022-03-02%2015/1。我想提取整个星期一整天的数据。 (我假设为此需要某种嵌套循环)。谁能给我一些指导。谢谢

json 2022 年 2 月 3 日的示例:

[ {
  "@id" : "12233071",
  "definition" : {
    "parkingRecord" : {
      "@xmlns" : "http://datex2.eu/schema/2/2_0",
      "@xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
      "parkingRecord" : {
        "@xsi:type" : "UrbanParkingSite",
        "@id" : "12233071",
        "@version" : "2.5",
        "parkingName" : {
          "values" : {
            "value" : {
              "@lang" : "en",
              "#text" : "210: Wilkinson Street Park and Ride"
            }
          }
        },
        "parkingDescription" : {
          "values" : {
            "value" : {
              "@lang" : "en",
              "#text" : "Wilkinson Street Park and Ride"
            }
          }
        },
        "parkingRecordVersionTime" : "2022-03-02T15:00:09.089Z",
        "parkingNumberOfSpaces" : "600",
        "parkingLocation" : {
          "@xsi:type" : "Point",
          "locationForDisplay" : {
            "latitude" : "52.97",
            "longitude" : "-1.18"
          }
        },
        "parkingThresholds" : {
          "almostFullDecreasing" : "80",
          "almostFullIncreasing" : "85",
          "fullDecreasing" : "90",
          "fullIncreasing" : "95"
        },
        "urbanParkingSiteType" : "offStreetParking"
      }
    }
  },
  "status" : {
    "parkingRecord" : {
      "@xmlns" : "http://datex2.eu/schema/2/2_0",
      "@xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
      "parkingRecordStatus" : {
        "@xsi:type" : "ParkingSiteStatus",
        "parkingRecordReference" : {
          "@targetClass" : "ParkingRecord",
          "@id" : "12233071",
          "@version" : "2.5"
        },
        "parkingStatusOriginTime" : "2020-03-09T12:17:42.000Z",
        "parkingOccupancy" : {
          "parkingNumberOfOccupiedSpaces" : "227",
          "parkingOccupancy" : "37.83",
          "vehicleCountAndRate" : {
            "measurementTimeDefault" : "2022-03-02T14:58:57.000Z",
            "vehicleRate" : {
              "measurementOrCalculationTime" : "2022-03-02T14:58:57.000Z",
              "fillRate" : {
                "vehicleFlowRate" : "0"
              },
              "exitRate" : {
                "vehicleFlowRate" : "1"
              }
            }
          }
        },
        "parkingSiteStatus" : "other",
        "parkingSiteOpeningStatus" : "closed"
      }
    }
  }
}]

这应该可以回答您关于构建所需 URL 的问题:

import datetime
prefix= "http://website/example/example/"
start = datetime.datetime(2022, 1, 1)
delta = datetime.timedelta(7)
end = start + delta
hourdelta = datetime.timedelta(hours=1)
urls = []
while start < end:
    urls.append(prefix + start.strftime("%Y-%m-%d%%20%H/1"))
    start += hourdelta
[print(urls[i]) for i in range(4)]
print()
[print(urls[len(urls) - 4 + i]) for i in range(4)]

示例输出(前 4 个 URL 和后 4 个 URL):

http://website/example/example/2022-01-01%2000/1
http://website/example/example/2022-01-01%2001/1
http://website/example/example/2022-01-01%2002/1
http://website/example/example/2022-01-01%2003/1

http://website/example/example/2022-01-07%2020/1
http://website/example/example/2022-01-07%2021/1
http://website/example/example/2022-01-07%2022/1
http://website/example/example/2022-01-07%2023/1

要遍历这些 URL,以 json 格式提取数据并将其存储在 csv 文件中,我建议查看