This commit is contained in:
louiscklaw
2025-01-31 21:57:33 +08:00
parent 52904f5284
commit 6c95c5e3ea
39 changed files with 32542 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
# https://api.data.gov.hk/v1/carpark-info-vacancy?data=vacancy&vehicleTypes=privateCar
import requests
from pprint import pprint
url = "https://api.data.gov.hk/v1/carpark-info-vacancy"
params = {
"data": "vacancy",
"vehicleTypes": "privateCar"
}
response = requests.get(url, params=params)
js_result = response.json()['results']
output = []
for js_row in js_result:
if ((type(js_row.get('privateCar')) == type([]) ) and len(js_row.get('privateCar'))> 0):
if (js_row.get('privateCar')[0].get("vacancy_type",'-')== "A"):
output.append(js_row)
test_json = [{
'park_Id': '10',
'privateCar': [{
'vacancy_type': 'A',
'vacancy': 29,
'lastupdate': '2024-10-18 16:04:59'
}]
}]
import pandas as pd
# Create a DataFrame from the test_json
df = pd.json_normalize(test_json, record_path=['privateCar'], meta='park_Id')
# Extract the 'vacancy' column and flatten it
vacancy_df = df[['park_Id', 'vacancy']]
print(vacancy_df)
pprint(output[0])
pprint("length of json: "+str(len(output)))
import json
# Store output into task3_output.json
with open('task3_output.json', 'w') as outfile:
json.dump(output, outfile, indent=2)