๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐™‹๐™ฎ๐™ฉ๐™๐™ค๐™ฃ/๐˜ผ๐™‹๐™„

ํŒŒ์ด์ฌ ๊ธฐ์ƒ์ฒญ ๋‹จ๊ธฐ์˜ˆ๋ณด API ์‚ฌ์šฉํ•ด๋ณด๊ณ  ํ™œ์šฉํ•˜๊ธฐ

by beomcoder 2023. 2. 20.
728x90
๋ฐ˜์‘ํ˜•

์šฐ๋ฆฌ ์–ดํ”Œ์—์„œ ํ˜„์žฌ ๋ชจ์ž„์„ ๋งŒ๋“ค ๋•Œ ๋‹ฌ๋ ฅ์— ๋‚ ์„ ์ฒดํฌํ•˜์—ฌ ๋ชจ์ž„ ์ผ์ •์„ ์ƒ์„ฑํ•œ๋‹ค.

ํ˜„์žฌ๋Š” ๊ทธ๋ƒฅ ๋‹ฌ๋ ฅ์— ์•„๋ฌด๋Ÿฐ ํ‘œ์‹œ๊ฐ€ ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค.

 

์ด๋•Œ ๋‹ฌ๋ ฅ์— ํ•ด๋‹น ๋‚ ์งœ์˜ ๋‚ ์”จ๊ฐ€ ์ ํ˜€์žˆ๋‹ค๋ฉด ๋‚ ์„ ์„ ์ •ํ•˜๋Š”๋ฐ ๋„์›€์ด ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์–ด ์ƒ๊ฐํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

๊ธฐ์ƒ์ฒญ API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์žฅ์†Œ๋ฅผ ๊ณ ๋ฅด๋ฉด 10์ผ๊นŒ์ง€์˜ ๋‚ ์”จ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ค‘๊ธฐ์œก์ƒ์˜ˆ๋ณด API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€์˜ ์•„์ด๋””์–ด๋Š” ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

 

 

ํŒŒ์ด์ฌ์œผ๋กœ ๊ธฐ์ƒ์ฒญ ์ผ๊ธฐ์˜ˆ๋ณด์™€ ๋‚ ์”จ API ์‚ฌ์šฉํ•˜๊ธฐ

๊ธฐ์ƒ์ฒญ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋‚ ์”จ API๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ํ˜„์žฌ ๋‚ด๊ฐ€ ๋‹ค๋‹ˆ๊ณ  ์žˆ๋Š” ํšŒ์‚ฌ๋Š” ๋ชจ์ž„ํ”Œ๋ž˜ํผ์„ ์šด์˜ํ•˜๊ณ  ์žˆ๋‹ค. ์–ดํ”Œ์—์„œ๋Š” ๋ชจ์ž„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ ๋‚ ์งœ์™€ ์‹œ๊ฐ„, ์žฅ์†Œ๋ฅผ ์ ๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ชจ์ž„์„

beomcoder.tistory.com

 

๊ทธ๋ฆฌ๊ณ  ๋ชจ์ž„๋‚ ์งœ๊ฐ€ ๋˜๋ฉด ๋ชจ์ž„์ด ์‹œ์ž‘๋˜๊ธฐ 6์‹œ๊ฐ„ ์ „์— ๋ฏธ๋ฆฌ ์•Œ๋ฆผ์œผ๋กœ ํ•ด๋‹น ๋ชจ์ž„์ธ์›๋“ค์—๊ฒŒ ์•Œ๋ฆผ์„ ๋ณด๋‚ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

์•„๋ž˜ ์‚ฌ์ง„์€ ์•„์ง ๋„์ž…๋˜์ง€ ์•Š์•„์„œ ๋‚ด๊ฐ€ ์ž„์˜๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

 

 

๋จผ์ € ๋ฐ์ดํ„ฐํฌํ„ธ์— ๋“ค์–ด๊ฐ€์„œ APIํ‚ค๋ฅผ ๋ฐ›๋Š”๋‹ค. ํ‚ค๋ฅผ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์— ์˜ฌ๋ ค๋‘” ๊ฒŒ์‹œ๊ธ€์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๊ธฐ์ƒ์ฒญ_๋‹จ๊ธฐ์˜ˆ๋ณด ((๊ตฌ)_๋™๋„ค์˜ˆ๋ณด) ์กฐํšŒ์„œ๋น„์Šค

์ดˆ๋‹จ๊ธฐ์‹คํ™ฉ, ์ดˆ๋‹จ๊ธฐ์˜ˆ๋ณด, ๋‹จ๊ธฐ((๊ตฌ)๋™๋„ค)์˜ˆ๋ณด, ์˜ˆ๋ณด๋ฒ„์ „ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ์ดˆ๋‹จ๊ธฐ์‹คํ™ฉ์ •๋ณด๋Š” ์˜ˆ๋ณด ๊ตฌ์—ญ์— ๋Œ€ํ•œ ๋Œ€ํ‘œ AWS ๊ด€์ธก๊ฐ’์„, ์ดˆ๋‹จ๊ธฐ์˜ˆ๋ณด๋Š” ์˜ˆ๋ณด์‹œ์ ๋ถ€ํ„ฐ 6์‹œ๊ฐ„๊นŒ์ง€์˜ ์˜ˆ๋ณด๋ฅผ,

www.data.go.kr

๋‹จ๊ธฐ์˜ˆ๋ณด ์กฐํšŒ์„œ๋น„์Šค๋Š” Decoding key๊ฐ€ ์ ์šฉ๋˜๋Š” ๋“ฏ ํ•˜๋‹ค. ์ผ๋ฐ˜ ์ธ์ฆํ‚ค๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ ์–ด๋†“๋Š”๋‹ค.

 

!pip install requests
!pip install beautifulsoup4
!pip install json

 

def weather(target):
    def now_weather_info():
        url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst'
        params = {
            'serviceKey' : '์•„๊นŒ ์ ์–ด๋†“์€ ๋ณธ์ธ์˜ ํ‚ค๊ฐ’', 
            'pageNo' : '1', 
            'numOfRows' : '1000',
            'dataType' : 'JSON', 
            'base_date' : '20230220', 
            'base_time' : '1500', 
            'nx' : '58', 
            'ny' : '75' 
        }

        r = requests.get(url, params=params)
        response = json.loads(r.content)
        print(response)

    def today_gathering():
        url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtFcst'
        params = {
            'serviceKey' : '์•„๊นŒ ์ ์–ด๋†“์€ ๋ณธ์ธ์˜ ํ‚ค๊ฐ’', 
            'pageNo' : '1', 
            'numOfRows' : '1000',
            'dataType' : 'JSON', 
            'base_date' : '20230220', 
            'base_time' : '0900', 
            'nx' : '58', 
            'ny' : '75' 
        }
        r = requests.get(url, params=params)
        response = json.loads(r.content)
        print(response)

    _function = {'now_weather_info': now_weather_info, 'today_gathering': today_gathering}

    _function[target]()

# gwangju('festival')
weather('today_gathering')

 

๋‚ ์”จ api๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋กœ ๊ธฐ๋Šฅ๋“ค์„ ๋ฌถ์–ด์ฃผ์—ˆ๋‹ค.

๋‚ด๊ฐ€ ์‚ฌ์šฉ์€ ํ•˜์ง€ ์•Š์„ ๊ฑฐ์ง€๋งŒ ์šฐ์„  ํ™•์ธํ•ด๋ณด๋ ค๊ณ  ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

now_weather_info()๋Š” ์˜ˆ์ธก์ •๋ณด๋ผ๊ธฐ๋ณด๋‹ค๋Š” ํ˜„์žฌ์˜ ์ •๋ณด๋“ค์„ ๋ณด์—ฌ์ค€๋‹ค.

2023๋…„ 02์›” 20์ผ์˜ ๋‚ ์”จ ์ค‘, ํ˜„์žฌ ์‹œ๊ฐ„์ด 16์‹œ๋ผ๋ฉด base_date๋Š” 20230220, base_time์— 1500์„ ์ ์–ด์ค€๋‹ค.

nx, ny๋Š” ์•„๋ž˜์—์„œ ํ•œ๊บผ๋ฒˆ์— ์•Œ๋ ค์ฃผ๋ ค๊ณ  ํ•œ๋‹ค.

 

params = {
            'serviceKey' : '์•„๊นŒ ์ ์–ด๋†“์€ ๋ณธ์ธ์˜ ํ‚ค๊ฐ’', 
            'pageNo' : '1', 
            'numOfRows' : '1000',
            'dataType' : 'JSON', 
            'base_date' : '20230220', 
            'base_time' : '0900', 
            'nx' : '58', 
            'ny' : '75' 
        }

 

today_gathering()ํ•จ์ˆ˜ ๋‚ด api ํ˜ธ์ถœ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•ด ์•Œ๋ ค์ฃผ๋ คํ•œ๋‹ค.

๋จผ์ € ํ•จ์ˆ˜๋Š” ์ž์œ ๋กญ๊ฒŒ ์ด๋ฆ„์„ ์ง€์œผ๋ฉด ๋˜๋Š”๋ฐ

๋‚˜๋Š” ์–ดํ”Œ์˜ ์˜ค๋Š˜ ๋ชจ์ž„ ์•Œ๋ฆผ์ด๋ฏ€๋กœ ํ•จ์ˆ˜์ด๋ฆ„์„ today_gathering์œผ๋กœ ์ง€์—ˆ๋‹ค.

 

url์€ ํ˜ธ์ถœ์ฃผ์†Œ์ด๊ณ , params๋Š” ์–ด๋–ค ์ •๋ณด๋ฅผ ์š”๊ตฌํ• ์ง€ ์ ๋Š” ๊ณณ์ด๋‹ค.

'serviceKey'๋Š” ๋ณธ์ธ์˜ ํ‚ค๊ฐ’์„ ์ ์œผ๋ฉด ๋œ๋‹ค. ์ธ์ฝ”๋”ฉ, ๋””์ฝ”๋”ฉ ํ‚ค๋ฅผ ๋‘˜๋‹ค ๋„ฃ์–ด๋ณด๊ณ  ๋˜๋Š” ๊ฐ’์„ ์ ์œผ๋ฉด ๋œ๋‹ค.

'pageNo' , 'numOfRows'๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ฐ’์„ ๋ฐ”๊ฟ”๋ด๋„ ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

'dataType'์€ ๋ฆฌํ„ด๊ฐ’์„ 'JSON'ํ˜•์‹์œผ๋กœ ๋ฐ›์„์ง€ 'XML'ํ˜•์‹์œผ๋กœ ๋ฐ›์„์ง€ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

'base_date'๋Š” ๋‚ ์งœ๋ฅผ ์ ์œผ๋ฉด ๋˜๋Š”๋ฐ ์ตœ๊ทผ 3์ผ๊ฐ„์˜ ์ž๋ฃŒ๋งŒ ์ œ๊ณต๊ฐ€๋Šฅํ•˜๋‹ค.

{'response': {'header': {'resultCode': '10', 'resultMsg': '์ตœ๊ทผ 3์ผ ๊ฐ„์˜ ์ž๋ฃŒ๋งŒ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.'}}}

{'response': {'header': {'resultCode': '03', 'resultMsg': 'NO_DATA'}}}

๋„ˆ๋ฌด ์˜ˆ์ „์˜ ๋‚ ์งœ๋‚˜ ๋ฏธ๋ž˜์˜ ์‹œ๊ฐ„์ด๋‚˜ ๋‚ ์งœ๋ฅผ ์ ์œผ๋ฉด ์œ„์˜ ์‘๋‹ต์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

 

'base_time'์€ ํ˜„์žฌ ์‹œ๊ฐ„๋ณด๋‹ค ์•ฝ 30๋ถ„์—์„œ 1์‹œ๊ฐ„์ •๋„ ์ „์˜ ์‹œ๊ฐ„์„ ์ ๋Š”๊ฒŒ ์ข‹๋‹ค.

'rx', 'ry'๋Š” ๊ทธ ์ง€์—ญ์˜ x๊ฐ’, y๊ฐ’์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ธฐ์ƒ์ฒญ41_๋‹จ๊ธฐ์˜ˆ๋ณด ์กฐํšŒ์„œ๋น„์Šค_์˜คํ”ˆAPIํ™œ์šฉ๊ฐ€์ด๋“œ_๊ฒฉ์ž_์œ„๊ฒฝ๋„(20221027).xlsx
1.23MB

 

ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ๋ฉด ๋ณธ์ธ์ด ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ์ง€์—ญ์„ ์ฐพ์•„์„œ ๊ฒฉ์žx, ๊ฒฉ์žy๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์•„๋ž˜๋Š” ๊ณต์‹๋ฌธ์„œ์˜ ํ‘œ์ด๋‹ˆ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

 

 

{'response': {'header': {'resultCode': '00', 'resultMsg': 'NORMAL_SERVICE'}, 'body': {'dataType': 'JSON', 'items': {'item': [{'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'LGT', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'PTY', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '0', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '๊ฐ•์ˆ˜์—†์Œ', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '๊ฐ•์ˆ˜์—†์Œ', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '๊ฐ•์ˆ˜์—†์Œ', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '๊ฐ•์ˆ˜์—†์Œ', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '๊ฐ•์ˆ˜์—†์Œ', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'RN1', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '๊ฐ•์ˆ˜์—†์Œ', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'SKY', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '3', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'T1H', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '65', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '55', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '50', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '40', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '40', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'REH', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '40', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0.5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '1.4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '2.4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '2.9', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '3.1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'UUU', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '3.4', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '0.6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '0.2', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '-1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '-3.5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '-4.8', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VVV', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '-5.1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '219', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '263', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '293', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '321', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '328', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'VEC', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '327', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1000', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1100', 'fcstValue': '1', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1200', 'fcstValue': '3', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1300', 'fcstValue': '5', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1400', 'fcstValue': '6', 'nx': 58, 'ny': 75}, {'baseDate': '20230220', 'baseTime': '0930', 'category': 'WSD', 'fcstDate': '20230220', 'fcstTime': '1500', 'fcstValue': '6', 'nx': 58, 'ny': 75}]}, 'pageNo': 1, 'numOfRows': 1000, 'totalCount': 60}}}

 

์ œ๋Œ€๋กœ ์ž…๋ ฅํ•˜์—ฌ ์‹คํ–‰์‹œํ‚ค๋ฉด ์œ„์™€ ๊ฐ™์€ ํ˜ธ์ถœ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋˜๋Š”๋ฐ ์ •์‹ ์—†๋‹ค.

๋‚˜๋„ ์ฒ˜์Œ์— ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ  ๋ฌผ์Œํ‘œ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

๋จผ์ € ํ•˜๋‚˜์”ฉ ํ•˜๋ฉด ๋‚ด๊ฐ€ ์ ์€ ์‹œ๊ฐ„(base_time) ์ดํ›„ 6์‹œ๊ฐ„๊นŒ์ง€์˜ ์˜ˆ๋ณด๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  XX:30์œผ๋กœ ํ†ต์ผ๋˜๋Š” ๋“ฏ ํ•˜๋‹ค.

base_time ์„ 1000์œผ๋กœ ์ ์–ด๋„ 1030์œผ๋กœ ์ „์†ก๋œ๋‹ค.

 

์ด ๊ฐ’์„ ์กฐ๊ธˆ ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋ฐ”๊ฟ”๋ณด๊ฒ ๋‹ค.

1์‹œ๊ฐ„ ํ›„์˜ ๊ด‘์ฃผ ๋™๋ฆผ๋™ ์ผ๊ธฐ์˜ˆ๋ณด์ž…๋‹ˆ๋‹ค.
๋‚™๋ขฐ : 0
๊ฐ•์ˆ˜ํ˜•ํƒœ : ์—†์Œ        
1์‹œ๊ฐ„ ๊ฐ•์ˆ˜๋Ÿ‰ : ๊ฐ•์ˆ˜์—†์Œ
ํ•˜๋Š˜์ƒํƒœ : ๋ง‘์Œ        
๊ธฐ์˜จ : 6
์Šต๋„ : 55
๋™์„œ๋ฐ”๋žŒ์„ฑ๋ถ„ : 1.2     
๋‚จ๋ถ๋ฐ”๋žŒ์„ฑ๋ถ„ : -0.1    
ํ’ํ–ฅ : 277
ํ’์† : 1

2์‹œ๊ฐ„ ํ›„์˜ ๊ด‘์ฃผ ๋™๋ฆผ๋™ ์ผ๊ธฐ์˜ˆ๋ณด์ž…๋‹ˆ๋‹ค.
๋‚™๋ขฐ : 0
๊ฐ•์ˆ˜ํ˜•ํƒœ : ์—†์Œ
1์‹œ๊ฐ„ ๊ฐ•์ˆ˜๋Ÿ‰ : ๊ฐ•์ˆ˜์—†์Œ
ํ•˜๋Š˜์ƒํƒœ : ๊ตฌ๋ฆ„๋งŽ์Œ
๊ธฐ์˜จ : 7
์Šต๋„ : 50
๋™์„œ๋ฐ”๋žŒ์„ฑ๋ถ„ : 2.3
๋‚จ๋ถ๋ฐ”๋žŒ์„ฑ๋ถ„ : -0.6
ํ’ํ–ฅ : 287
ํ’์† : 2

3์‹œ๊ฐ„ ํ›„์˜ ๊ด‘์ฃผ ๋™๋ฆผ๋™ ์ผ๊ธฐ์˜ˆ๋ณด์ž…๋‹ˆ๋‹ค.
๋‚™๋ขฐ : 0
๊ฐ•์ˆ˜ํ˜•ํƒœ : ์—†์Œ
1์‹œ๊ฐ„ ๊ฐ•์ˆ˜๋Ÿ‰ : ๊ฐ•์ˆ˜์—†์Œ
ํ•˜๋Š˜์ƒํƒœ : ๋ง‘์Œ
๊ธฐ์˜จ : 7
์Šต๋„ : 40
๋™์„œ๋ฐ”๋žŒ์„ฑ๋ถ„ : 2.8
๋‚จ๋ถ๋ฐ”๋žŒ์„ฑ๋ถ„ : -2.5
ํ’ํ–ฅ : 313
ํ’์† : 4

(์ค‘๋žต)

์™œ ํ’์†์ด 1,2,3,4 ... ๋กœ ๊ฐ€๋Š”์ง€๋Š” ์šฐ์—ฐ์ธ์ง€ api๊ฐ€ ์ž˜๋ชป๋œ๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. 

 

def weather(target):
    def today_gathering():
        url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtFcst'
        params = {
            'serviceKey' : '๋ณธ์ธ์˜ ํ‚ค๊ฐ’', 
            'pageNo' : '1', 
            'numOfRows' : '1000',
            'dataType' : 'JSON', 
            'base_date' : '20230220',
            'base_time' : '1000', 
            'nx' : '58', 
            'ny' : '75' 
        }
        r = requests.get(url, params=params)
        response = json.loads(r.content)

        if response['response']['header']['resultCode'] != '00':
            return response['response']['header']['resultMsg']
        else:
            code_name = {'T1H': '๊ธฐ์˜จ', 'RN1': '1์‹œ๊ฐ„ ๊ฐ•์ˆ˜๋Ÿ‰', 'SKY': 'ํ•˜๋Š˜์ƒํƒœ', 'UUU': '๋™์„œ๋ฐ”๋žŒ์„ฑ๋ถ„', 'VVV': '๋‚จ๋ถ๋ฐ”๋žŒ์„ฑ๋ถ„',
                         'REH': '์Šต๋„', 'PTY': '๊ฐ•์ˆ˜ํ˜•ํƒœ', 'LGT': '๋‚™๋ขฐ', 'VEC': 'ํ’ํ–ฅ', 'WSD': 'ํ’์†'}
            sky_value = {'1': '๋ง‘์Œ', '3': '๊ตฌ๋ฆ„๋งŽ์Œ', '4': 'ํ๋ฆผ'}
            pty_value = {'0': '์—†์Œ', '1': '๋น„', '2': '๋น„/๋ˆˆ', '3': '๋ˆˆ', '5': '๋น—๋ฐฉ์šธ', '6': '๋น—๋ฐฉ์šธ/๋ˆˆ๋‚ ๋ฆผ', '7': '๋ˆˆ๋‚ ๋ฆผ'}
            
            result = {'1': {}, '2': {}, '3': {}, '4': {}, '5': {}, '6': {}}
            
            for i, v in enumerate(response['response']['body']['items']['item']):
                if v['category'] == 'SKY':
                    result[str(i%6 + 1)][code_name[v['category']]] = sky_value[v['fcstValue']]
                elif v['category'] == 'PTY':
                    result[str(i%6 + 1)][code_name[v['category']]] = pty_value[v['fcstValue']]
                else:
                    result[str(i%6 + 1)][code_name[v['category']]] = v['fcstValue']
            

        return result
	
    _function = {'today_gathering': today_gathering}

    return _function[target]()

# gwangju('festival')
result = weather('today_gathering')

for i in result.keys():
    print(i+'์‹œ๊ฐ„ ํ›„์˜ ๊ด‘์ฃผ ๋™๋ฆผ๋™ ์ผ๊ธฐ์˜ˆ๋ณด์ž…๋‹ˆ๋‹ค.')
    for k, v in result[i].items():
        print(k, ':', v)
    print()

 

์ผ๋‹จ์€ ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”๋ณด์•˜๋Š”๋ฐ ํ’์†, ํ’ํ–ฅ, ์Šต๋„ ๋“ฑ์— ๊ธฐํ˜ธ๋ฅผ ์•Œ๋งž๊ฒŒ ์ถ”๊ฐ€๋กœ ๋„ฃ์œผ๋ฉด ๋ ๋“ฏํ•˜๋‹ค.

์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ๋„ ๋œ๋‹ค.

 

๊ธฐ์ƒ์ฒญ41_๋‹จ๊ธฐ์˜ˆ๋ณด ์กฐํšŒ์„œ๋น„์Šค_์˜คํ”ˆAPIํ™œ์šฉ๊ฐ€์ด๋“œ_์ตœ์ข….docx
0.64MB

์˜คํ”ˆ api ๊ฐ€์ด๋“œ๋ฅผ ๋ณด๊ณ  ๋งŒ๋“ค์–ด๋ณด์•„๋„ ๋œ๋‹ค.

๋‚ด๊ฐ€ ๋งŒ๋“  ์ฝ”๋“œ๋Š” ์ œ์•ˆ์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ž ๊น ๋งŒ๋“  ์ฝ”๋“œ๋ผ ํ˜„์žฌ ์—‰์„ฑํ•˜๋‹ค.

๋Œ€ํ‘œ๋‹˜๊ป˜์„œ ์ง„ํ–‰ํ•˜๋ผ๊ณ  ํ•˜์‹œ๋ฉด FEํŒ€๊ณผ ์ƒ์˜ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์งœ์•ผ ํ•œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€