AQI 相關範例網址調整

針對政府資料開放平臺中 AQI 空氣品質指標的 API 版本從 v1 改到 v2,新版雖然僅將 key 全部由大寫改為小寫,日期格式也非標準,但為了配合 MongoDB 書中跟 AQI 有關的範例,請讀者將 3-15 頁程式碼中呼叫 API 網址改為我幫各位準備好的網址,如下(網址在書中的 3-16 頁)。

https://raw.githubusercontent.com/kirkchu/mongodb/main/aqi.json

此網址已經將 v2 格式還原為 v1 格式,這樣書中跟 AQI 有關的範例就不需要調整了。

import urllib.request as urllib
import json
import pymongo

url = 'https://raw.githubusercontent.com/kirkchu/mongodb/main/aqi.json'

# 下載 JSON 資料並解析
response = urllib.urlopen(url)
text = response.read().decode('utf-8')
print(text)
text = text.replace('PM2.5', 'PM2_5')
text = text.replace('"AQI": ""', '"AQI": "-1"')
jsonObj = json.loads(text)

# 將資料存進opendata資料庫中的AQI資料表
client = pymongo.MongoClient()
db = client.opendata
db.AQI.insert_many(jsonObj['records'])

發表迴響