1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

東京都が公開しているAPIで毎日コロナ感染者数を取得するコード②

Last updated at Posted at 2021-05-20

前回公開した東京都が公開しているAPIで毎日コロナ感染数を取得するコードにてうまく1000件以上の件数が取れなかったためその対応を行なった。
コードは以下の通り

tokyo_corona.py
import requests
import datetime
import pandas as pd
import json

today = datetime.date.today().strftime('%Y-%m-%d')
url = "https://api.data.metro.tokyo.lg.jp/v1/Covid19Patient"

def corona_infection(start_date=today, end_date=today):
    payload ={
        'from': start_date,
        'till': end_date,
        'limit': 1000
    }
    
    res = requests.get(url, params=payload)
    data = res.json()
    
    infect_data = data[0]
    endCursor = data[1]
    
    df = pd.DataFrame(infect_data)
    total = df.shape[0]
    
    while True:
        if endCursor['moreResults'] == 'MORE_RESULTS_AFTER_LIMIT':
            payload['cursor'] = endCursor['endCursor']
            res = requests.get(url, params=payload)
            data = res.json()
            
            infect_data = data[0]
            endCursor = data[1]
            
            df = pd.DataFrame(infect_data)
            over_count = df.shape[0]
            total += over_count
        else:
            break
    return total
実行方法
corona_infection('2021-05-19','2021-05-19')

上記の通りcorona_infection関数の第1引数と第2引数に開始日と終了日を指定する
※ただし引数を省略した場合は当日のデータを取ってくる

これで予定通り感染者の総計を指定して取れるようになったため、
次はmatplotlib等を使ってグラフ化等を行なっていこうと思う。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?