재우니의 블로그

python - 2022년 국가공휴일 데이터 가져오기 (data.go.kr)

 

 

data.go.kr/

 

공공데이터 포털

국가에서 보유하고 있는 다양한 데이터를『공공데이터의 제공 및 이용 활성화에 관한 법률(제11956호)』에 따라 개방하여 국민들이 보다 쉽고 용이하게 공유•활용할 수 있도록 공공데이터(Datase

data.go.kr


오퍼레이션 정보

 

일련번호 서비스명(국문) 오퍼레이션명(영문) 오퍼레이션명(국문)
1 특일 정보제공 서비스 getHoliDeInfo 국경일 정보조회
2 getRestDeInfo 공휴일 정보조회
3 getAnniversaryInfo 기념일 정보조회
4 get24DivisionsInfo 24절기 정보조회
5 getSundryDayInfo 잡절 정보조회

 

xml 형태

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
	<header>
		<resultCode>00</resultCode>
		<resultMsg>NORMAL SERVICE.</resultMsg>
	</header>
	<body>
		<items>
			<item>
				<dateKind>01</dateKind>
				<dateName>\xec\x8b\xa0\xec\xa0\x95</dateName>
				<isHoliday>Y</isHoliday>
				<locdate>20100101</locdate>
				<seq>1</seq>
			</item>
		</items>
		<numOfRows>10</numOfRows>
		<pageNo>1</pageNo>
		<totalCount>1</totalCount>
	</body>
</response>'

 

파이썬 코딩

 

import requests
import datetime
from bs4 import BeautifulSoup

def print_whichday(year, month, day) :
    r = ['월요일', '화요일', '수요일', '목요일', '금요일', '토요일', '일요일']
    aday = datetime.date(year, month, day)
    bday = aday.weekday()
    return r[bday]

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

year = 2022
#일반 인증키(Encoding)
mykey = "WoMViKPOmQYKGqkJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

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')
        #print(item);
        for i in item:
            
            day = int(i.locdate.string[-2:])
            weekname = print_whichday(int(year), int(month), day)
            print(i.datename.string, i.isholiday.string, i.locdate.string, weekname)

 

2022 년 출력

 

PS C:\Users\lucks> & d:/Python/Python39/python.exe d:/source/pythonexam/birth.py
1월1일 Y 20220101 토요일
설날 Y 20220131 월요일
설날 Y 20220201 화요일
설날 Y 20220202 수요일
삼일절 Y 20220301 화요일
대통령선거일 Y 20220309 수요일
어린이날 Y 20220505 목요일
부처님오신날 Y 20220508 일요일
전국동시지방선거 Y 20220601 수요일
현충일 Y 20220606 월요일
광복절 Y 20220815 월요일
추석 Y 20220909 금요일
추석 Y 20220910 토요일
추석 Y 20220911 일요일
대체공휴일 Y 20220912 월요일
개천절 Y 20221003 월요일
한글날 Y 20221009 일요일
대체공휴일 Y 20221010 월요일
기독탄신일 Y 20221225 일요일

 

2023년 공휴일

 

1월1일 Y 20230101 일요일
설날 Y 20230121 토요일
설날 Y 20230122 일요일
설날 Y 20230123 월요일
대체공휴일 Y 20230124 화요일
삼일절 Y 20230301 수요일
어린이날 Y 20230505 금요일
부처님오신날 Y 20230527 토요일
현충일 Y 20230606 화요일
광복절 Y 20230815 화요일
추석 Y 20230928 목요일
추석 Y 20230929 금요일
추석 Y 20230930 토요일
개천절 Y 20231003 화요일
한글날 Y 20231009 월요일
기독탄신일 Y 20231225 월요일

 

 

참고 사이트

 

mwultong.blogspot.com/2007/01/python-int-long-float-string-to-number.html

 

Python/파이썬] 문자열을 숫자로 변환; 문자를 정수(int, long), 실수(float)로 바꾸기; String to Number

"123" 이렇게 따옴표에 들어 있는 숫자는, 문자열이지 숫자가 아닙니다. 이것을 123 이런 진짜 숫자로 만드는 방법입니다. 문자를, 진짜 숫자로 변환 예제 주의: 파이선 2.x 용 소스임 #!/usr/bin/python #

mwultong.blogspot.com

hashcode.co.kr/questions/8174/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EB%82%A0%EC%A7%9C%EB%A5%BC-%EC%9E%85%EB%A0%A5%ED%95%98%EB%A9%B4-%EC%9A%94%EC%9D%BC%EC%9D%84-%EC%95%8C%EB%A0%A4%EC%A3%BC%EB%8A%94-%EC%BD%94%EB%93%9C

 

파이썬 날짜를 입력하면 요일을 알려주는 코드

연 월 일 을 기입하면 요일을 알려주는 코드를 짜는중입니다. 일단은 import datetime def print_whichday(year,month,date) r=['월요일','화요일','수요일','목요일','금요일','토요일','일요일'] aday=datetime.date(year,

hashcode.co.kr

freeharmony.tistory.com/64

 

[BeautifulSoup] Python 으로 xml 처리

Python에서 xml을 파싱하여 처리하는 간단한 방법을 소개합니다. 각종 사이트에서 전달되는 RSS(Rich Site Summary) 컨텐츠를 가지고 쉽게 재가공 하여 사용할 수 있도록 도와주는 외부 모듈입니다. 최

freeharmony.tistory.com