from urllib.request import Request, urlopen
from urllib.parse import urlencode, quote_plus
from xml.etree import ElementTree
import pandas as pd
url = 
service_key = 
params = '?' + \
    urlencode({
        quote_plus('ServiceKey') : service_key,
        quote_plus('pageNo') : '0',
        # quote_plus('num0fRows') : '20',
        quote_plus('startCreateDt') : '2020',
        quote_plus('endCreateDt') : '20220401',
    })
url+params
request = Request(url + params)
response_body = urlopen(request).read()
root = ElementTree.fromstring(response_body)
df = pd.DataFrame()
for item in root.iter('item'):
    item_dict = {}
    item_dict['기준일시'] = item.find('stdDay').text
    item_dict['시도명'] = item.find('gubun').text
    item_dict['확진자'] = item.find('defCnt').text
    item_dict['전일대비증감'] = item.find('incDec').text
    item_dict['해외유입'] = item.find('overFlowCnt').text
    item_dict['지역발생'] = item.find('localOccCnt').text
    item_dict['사망자수'] = item.find('deathCnt').text
    df = df.append(item_dict, ignore_index = True)    
df
기준일시 시도명 확진자 전일대비증감 해외유입 지역발생 사망자수
0 2022년 04월 01일 00시 검역 10703 33 33 0 16
1 2022년 04월 01일 00시 제주 168162 3873 0 3873 104
2 2022년 04월 01일 00시 경남 801564 17255 5 17250 758
3 2022년 04월 01일 00시 경북 507130 12974 2 12972 776
4 2022년 04월 01일 00시 전남 383445 11826 2 11824 209
... ... ... ... ... ... ... ...
15252 2020년 01월 20일 00시 전남 0 0 0 0 0
15253 2020년 01월 20일 00시 경북 0 0 0 0 0
15254 2020년 01월 20일 00시 경남 0 0 0 0 0
15255 2020년 01월 20일 00시 제주 0 0 0 0 0
15256 2020년 01월 20일 00시 검역 0 0 0 0 0

15257 rows × 7 columns

df['기준일시'] = pd.to_datetime(df['기준일시'], format = '%Y년 %m월 %d일 %H시')
df.sort_values(['기준일시'], inplace = True)
df.reset_index(drop = True, inplace = True)
df.head(19)
기준일시 시도명 확진자 전일대비증감 해외유입 지역발생 사망자수
0 2020-01-20 검역 0 0 0 0 0
1 2020-01-20 합계 1 0 1 0 0
2 2020-01-20 서울 0 0 0 0 0
3 2020-01-20 부산 0 0 0 0 0
4 2020-01-20 대구 0 0 0 0 0
5 2020-01-20 인천 1 0 1 0 0
6 2020-01-20 광주 0 0 0 0 0
7 2020-01-20 대전 0 0 0 0 0
8 2020-01-20 울산 0 0 0 0 0
9 2020-01-20 제주 0 0 0 0 0
10 2020-01-20 경기 0 0 0 0 0
11 2020-01-20 경남 0 0 0 0 0
12 2020-01-20 경북 0 0 0 0 0
13 2020-01-20 전남 0 0 0 0 0
14 2020-01-20 세종 0 0 0 0 0
15 2020-01-20 충남 0 0 0 0 0
16 2020-01-20 충북 0 0 0 0 0
17 2020-01-20 강원 0 0 0 0 0
18 2020-01-20 전북 0 0 0 0 0
df.to_csv('corona_kr.csv', encoding = 'utf-8-sig')