개발/ETC
[python] 공휴일 데이터 추출하기_API 활용
wonpick
2021. 6. 9. 08:58
공휴일 데이터를 api 활용하여 쉽게 가져오는 방법
1. 공공데이터 포털 접속 후 한국천문연구원_특일 정보 검색
2. 활용신청
3. 인증키 인코딩 정보를 하단 my_key란에 입력
끝!
#api
import requests
from bs4 import BeautifulSoup
import datetime
import pandas as pd
# 오류 Exceeded 30 redirects
response = requests.get(url, allow_redirects=False)
holiday_list = []
response = requests.get(url, allow_redirects=False)
def get_request_query(url, operation, params, serviceKey):
import urllib.parse as urlparse
params = urlparse.urlencode(params)
request_query = url + '/' + operation + '?' + params + '&' + 'serviceKey' + '=' + serviceKey
return request_query
mykey = 인증키입력
for year in [2019,2020,2021] :
for month in range(1,13):
if month < 10:
month = '0' + str(month)
else:
month = str(month)
url = 'http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService'
operation = 'getRestDeInfo'
params = {'solYear':year, 'solMonth':month}
request_query = get_request_query(url, operation, params, mykey)
get_data = requests.get(request_query)
if True == get_data.ok:
soup = BeautifulSoup(get_data.content, 'html.parser')
item = soup.findAll('item')
for i in item:
date = i.locdate.string
일자 = datetime.date(int(date[:4]), int(date[4:6]), int(date[6:]))
holiday_list.append(일자)
holiday_list = pd.DataFrame(holiday_list, columns = ['일자'])
holiday_list['일자'] = pd.to_datetime(holiday_list['일자'])
holiday_list['holiday'] = 1
holiday_list