๐ฅ open API ์ดํด
๐ฅ ๋ค์ด๋ฒ developers ํ์ฉํ์ฌ ๋ฐ์ดํฐ JSON ํํ๋ก ํด๋ก๋งํ๊ธฐ
๐ฅ ์ ๋ถ์ ๊ณต Open API ์ฌ์ฉํ๊ธฐ
1) open API(Rest API)๋?
- API: Aplication Programming Interface์ ์ฝ์๋ก, ํ๋ก๊ทธ๋จ์ ๋ง๋ค๊ธฐ ์ํด ์ ๊ณต๋๋ ๋ชจ๋(ํจ์ ๋ฑ)์ ์๋ฏธ
- Open API : ๋๊ตฌ๋ ์ฌ์ฉํ ์ ์๋๋ก ๊ณต๊ฐ๋ API(์ฃผ๋ก REST API ๊ธฐ์ ์ ์ฌ์ฉํจ)
- REST API : Representational State Transfer API์ ์ฝ์๋ก, HTTP ํ๋กํ ์ปฌ์ ํตํด ์๋ฒ ์ ๊ณต ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ ํจ์
- REST API๋ ์ผ๋ฐ์ ์ผ๋ก XML, JSON์ ํํ๋ก ์๋ต์ ์ ๋ฌ
2) JSON ์ด๋?
- JavaScript Object Notation ์ค์๋ง
- ์น ํ๊ฒฝ์์ ์๋ฒ์ ํด๋ผ์ด์ธํธ ์ฌ์ด์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ๋ ์ฃผ๋ก ์ฌ์ฉ
- JSON ํฌ๋งท ์ : dict๊ณผ ๊ฐ์ ํํ
- {"id":"01", "language":"python", "edition":"third", "author":"jaewon"}
- key์๋ ๋ฌธ์์ด์ด ๋ค์ด๊ฐ์ง๋ง, value์๋ ๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ๋ด์ ์ ์์
- JSON๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ์ค์น ํ์ : pip3 install json
1) ๋ค์ด๋ฒ ๋ด์ค ๊ฒ์ ๊ฒฐ๊ณผ ํฌ์คํธ๋งจ์ผ๋ก JSON ์ ๋ฌ๋ฐ์ ๋ค๋ฃจ๊ธฐ
- โญ๏ธ naver developers ์ฐธ๊ณ : https://developers.naver.com/main/
- ํฌ์คํธ๋งจ ์ค์น ํ ํ์ผ ์์ฑํ์ฌ params์ ๊ฐ์ ธ์ฌ ์ฃผ์ GET์์ ๋ฑ๋ก
- ์ดํ Headers์ developers.naver ๊ฐ์ ํ ๋ ๋ฐ์ Id์ Secret ๋ฑ๋ก
- id์ Secret ๋ฑ๋ก ํ ๋, KEY๊ฐ์ผ๋ก id์๋ X-Naver-Client-Id์ ์ ๊ณ , Secret์๋ X-Naver-Client-Secret๋ฅผ ์ ์ด VALUE์ ๊ฐ์ ์ ์ด์ฃผ๋ฉด ๋จ
- API๋ฅผ ๋ฐ์์ค๋ ์ธ์ดํธ๋ณ๋ก ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ํด๋น ์น์์ ์ ๊ณตํ๋ API ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ด๋ป๊ฒ ํธ์ถํ ์ ์๋์ง ์ํฉ์ ๋ฐ๋ผ ์ ์ฉํด์ผ ํจ
- SEND๋ฅผ ๋๋ฅด๋ฉด ์๋ BODY์ฐฝ์ JSONํํ๋ก ๋ฐ์ ๋ณด์ฌ์ค
โ๐ป python
# postman์ ์ด์ฉํ์ฌ ๋ค์ด๋ฒ ๋ด์ค์์ android ํค์๋๋ก ๊ฒ์ํ ๊ฒฐ๊ณผ import json data = """ { "lastBuildDate": "Fri, 05 Feb 2021 14:06:50 +0900", "total": 23784, "start": 1, "display": 10, "items": [ { "title": "'๊ทธ๋ ค๋ผ, ํฐ์น: ๋ ๊ฐ์ ์๊ตญ' ๋ฌด๋ฃ ์ ๋ฐ์ดํธ ์ ๊ณต", "originallink": "https://www.gamechosun.co.kr/webzine/article/view.php?no=172200", "link": "https://www.gamechosun.co.kr/webzine/article/view.php?no=172200", "description": "๋ฐ <b>Android</b>์์ ์ ๊ณตํ๋ค. ํฌ๋ฆฌ์์ดํฐ๋ธ ํฉ DLC๋ ๋ํ ๋ ์ค์์น ํ๋ ์ด์ด๋ฅผ ์ํ 10๊ฐ์ง ์๋ก์ด ์ฑ๋ฆฐ์ง... ๋ฐ <b>Android</b>์๋ง ํด๋น) โ๊ทธ๋ ค๋ผ, ํฐ์น: ๋ ๊ฐ์ ์๊ตญโ์ โ๊ทธ๋ ค๋ผ, ํฐ์น: ๋ ๋ฅ์คํธ ์ฑํฐโ๊ฐ ๋๋๊ณ ๋ช ๋ ... ", "pubDate": "Fri, 05 Feb 2021 11:33:00 +0900" }, { "title": "'๊ทธ๋ ค๋ผ, ํฐ์น: ๋ ๊ฐ์ ์๊ตญ' ๋ฌด๋ฃ DLC ํตํด ๋ํ ๋ ์ค์์น ์ ์ ์ํ ํฐ์น ...", "originallink": "http://gamefocus.co.kr/detail.php?number=114250", "link": "http://gamefocus.co.kr/detail.php?number=114250", "description": "๋ฐ <b>Android</b>์์ ์ ๊ณตํ๋ค. ํฌ๋ฆฌ์์ดํฐ๋ธ ํฉ DLC๋ ๋ํ ๋ ์ค์์น ํ๋ ์ด์ด๋ฅผ ์ํ 10๊ฐ์ง ์๋ก์ด ์ฑ๋ฆฐ์ง... ๋ฐ <b>Android</b>์๋ง ํด๋น) '๊ทธ๋ ค๋ผ, ํฐ์น: ๋ ๊ฐ์ ์๊ตญ'์ '๊ทธ๋ ค๋ผ, ํฐ์น: ๋ ๋ฅ์คํธ ์ฑํฐ'๊ฐ ๋๋๊ณ ๋ช ๋ ํ๋ฅผ... ", "pubDate": "Fri, 05 Feb 2021 10:45:00 +0900" }, { "title": "โ๊ตฌ๊ธ๋ฒ์ญโ์ผ๋ก ๋์ ์นผ๋ผ์ด ์์ ์๋ฌธํ โ์ธ๊ณต์ง๋ฅ์ ๋ณด๊ณ ๋๋ผโ", "originallink": "http://www.breaknews.com/784374", "link": "http://www.breaknews.com/784374", "description": "It is said to provide website interfaces for <b>Android</b> and iOS, mobile apps, and APIs that help developers develop browser extensions and application software. Other languages โโare translated into paragraphs of text... ", "pubDate": "Thu, 04 Feb 2021 10:37:00 +0900" }, { "title": "์ค์นด์ด์ค์บ๋, ์งํญ์์ ์์ธก B2B ์ ์ฉ โUnserved Routesโ ๋ชจ๋ ์ถ์ ํ๋ค.", "originallink": "http://www.tourtimes.net/353154", "link": "http://www.tourtimes.net/353154", "description": "์ค์นด์ด์ค์บ๋๋ ์ปดํจํฐ, ๋ชจ๋ฐ์ผ์์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ฑ(<b>Android</b>/iOS)์ 1์ต ๋ง๋ฒ์ ๋ค์ด๋ก๋๋ฅผ ๊ธฐ๋กํ๋ค. ์ค์นด์ด์ค์บ๋์ ๋ชฉํ๋ 1200์ฌ ์ฌํ ํํธ๋๋ค๊ณผ ํ๋ ฅํ์ฌ, ํ๋์ ์ด๊ณ ์ง์ ๊ฐ๋ฅํ ์ฌํ์ผ๋ก ์ธ๊ณ์ ์ธ ๋ณํ๋ฅผ... ", "pubDate": "Thu, 04 Feb 2021 09:35:00 +0900" }, { "title": "๋๋ฆผ์์ด์ค, ํญ์ค์ฝ MIH์ ์ฃผ์ ํํธ๋์ฌ๋ก ์ ์ ", "originallink": "http://www.businesskorea.co.kr/news/articleView.html?idxno=59828", "link": "http://www.businesskorea.co.kr/news/articleView.html?idxno=59828", "description": "์ค๋ฆฝ ์ด๊ธฐ๋ถํฐ ์ฑ๊ฐํฌ๋ฅด์์ ์ด๋ฆฐ '2015 ์ธํผ๋์ธ ๋ฒค์ฒํฌ๋ผ'์ ์ฐธ๊ฐ ํ๊ตญ ๊ธฐ์ ์ผ๋ก๋ ์ ์ผํ๊ฒ 2์๋ฅผ ์์ํ ์ ๋๋ก ์ผ์ฐ์ด ๊ธฐ์ ์ ์ธ์ ์ ๋ฐ์ ๊ธฐ์ ์ด๋ฉฐ, 2019๋ ์๋ AGL Summit 2019 (ALS 2019)์์ <b>Android</b> Container ๊ธฐ์ ์... ", "pubDate": "Thu, 04 Feb 2021 09:32:00 +0900" }, { "title": "What Can You Actually Buy With Bitcoin?", "originallink": "https://www.nytimes.com/2021/02/03/style/what-can-you-actually-buy-with-bitcoin.html?partner=naver", "link": "https://www.nytimes.com/2021/02/03/style/what-can-you-actually-buy-with-bitcoin.html?partner=naver", "description": "Additionally, apps in the Apple and <b>Android</b> stores make it possible for crypto-connoisseurs to pay for airline tickets by converting Bitcoin to fiat money in real time. On Monday, a news release... ", "pubDate": "Thu, 04 Feb 2021 01:23:00 +0900" }, { "title": "As Jeff Bezos Takes Off, Meet His Earthbound Successor", "originallink": "https://www.nytimes.com/2021/02/03/opinion/amazon-jeff-bezos-andy-jassy.html?partner=naver", "link": "https://www.nytimes.com/2021/02/03/opinion/amazon-jeff-bezos-andy-jassy.html?partner=naver", "description": "To hear more audio stories from publishers like The New York Times, download Audm for iPhone or <b>Android</b>. Who, you might ask at this moment, is Andy Jassy? Heโs a talented entrepreneur, for sure... ", "pubDate": "Wed, 03 Feb 2021 19:02:00 +0900" }, { "title": "Can We Please Stop Talking About Stocks, Please?", "originallink": "https://www.nytimes.com/2021/02/03/opinion/gamestop-stock-market-economy.html?partner=naver", "link": "https://www.nytimes.com/2021/02/03/opinion/gamestop-stock-market-economy.html?partner=naver", "description": "To hear more audio stories from publishers like The New York Times, download Audm for iPhone or <b>Android</b>. This column is not about GameStop, though not for lack of trying. Last week the aging... ", "pubDate": "Wed, 03 Feb 2021 19:02:00 +0900" }, { "title": "The Many Lives of Steven Yeun", "originallink": "https://www.nytimes.com/2021/02/03/magazine/steven-yeun.html?partner=naver", "link": "https://www.nytimes.com/2021/02/03/magazine/steven-yeun.html?partner=naver", "description": "To hear more audio stories from publishers like The New York Times, download Audm for iPhone or <b>Android</b>. When I was growing up in the โ90s, the only Asian-American writer I knew was Amy Tan.... ", "pubDate": "Wed, 03 Feb 2021 19:02:00 +0900" }, { "title": "Risking Everything to Come to America on the Open Ocean", "originallink": "https://www.nytimes.com/2021/02/03/magazine/customs-border-protection-migrants-pacific-ocean.html?partner=naver", "link": "https://www.nytimes.com/2021/02/03/magazine/customs-border-protection-migrants-pacific-ocean.html?partner=naver", "description": "To hear more audio stories from publishers like The New York Times, download Audm for iPhone or <b>Android</b>. The fiberglass skiff Lazora idled on the darkened Pacific Ocean a few miles south of... ", "pubDate": "Wed, 03 Feb 2021 19:02:00 +0900" } ] } """
json_data = json.loads(data) # json์์ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๋ ํจ์ : loads() print(json_data) print(json_data['lastBuildDate']) print(json_data['items']) print(json_data['items'][-1]['title']) print(json_data['items'][-1]['pubDate'])
2) ๋ค์ด๋ฒ ์ผํ ๊ฒ์ ๊ฒฐ๊ณผ vscode์์ JSON ์ ๋ฌ๋ฐ์ ๋ค๋ฃจ๊ธฐ
- ํฌ์คํธ๋งจ์์ JSON์ ์ ๋ฌ๋ฐ์ ๋ฐฉ๋ฒ์ ํ๋ก๊ทธ๋๋ฐ์ ํตํด ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์.
- pprint ๋ผ์ด๋ธ๋ฌ๋ฆฌ : JSON์ผ๋ก ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์กฐ์ ์ผ๋ก ์ ๋ ฌ๋์ ์ถ๋ ฅํด์ค
- ๊ธฐ์กด print()๋ก ์ฌ์ฉํ๋ ๋ถ๋ถ์ pprint.pprint() ๋ก ์ฌ์ฉํ๋ฉด ์ ๋ ฌ๋์ ์ถ๋ ฅํจ
- enumerate() ํจ์ : for๋ฌธ์์ index๋ฒํธ๋ฅผ ํจ๊ป ์ถ๋ ฅํด์ฃผ๋ ํจ์
โ๐ป python
import requests import pprint # json์ ๋ณด๊ธฐ ํธํ๊ฒ ๊ตฌ์กฐ์ ์ผ๋ก ์ ๋ ฌํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ client_id = '๋ด id์ ๋ ฅ' client_secret = '๋ด secret ์ ๋ ฅ' naver_open_api = 'https://openapi.naver.com/v1/search/shop.json?query=android' header_params = {"X-Naver-Client-Id":client_id, "X-Naver-Client-Secret":client_secret} # id์ secret์ json ํ์์ผ๋ก ๋ง๋ค์ด ๋ณ์์ ์ ์ฅ res = requests.get(naver_open_api, headers=header_params) # JSON ํ์์ผ๋ก ๋ฐ์์ฌ ์ฃผ์์ ๋ด ํค๋ ์ ๋ณด ํตํด ์์ฒญ # requests ํ ๊ฒฐ๊ณผ์ ์๋ต์ด ์ ์์ผ ๋์, ์ ์์ด์๋ ๋ if res.status_code == 200: data = res.json() # ์ ์์ ์ด๋ฉด ์ถ๋ ฅ pprint.pprint(data) # print()๋ก ๋ถ๋ฌ์ค๋ฉด data๊ฐ ๋ณด๊ธฐ ์ด๋ ค์. pprint๋ก ๋ถ๋ฌ์ค๋ฉด ๊ตฌ์กฐ์ ์ผ๋ก ๋ณด์ฌ์ค for index, item in enumerate(data['items']): # enumerate() ํจ์๋ฅผ ์จ์ ์ธ๋ฑ์ค ๋ฒํธ๋ ํจ๊ป ๋ณผ ์ ์์ print(index+1, item['title'], item['link']) else: print("Error Code: ", res.status_code) # ๋ฌธ์ ์์ผ๋ฉด ์๋ฌ ์ฝ๋
3) ์ํ ๊ฒ์ ์์ ๋ฆฌ์คํธ ์์ ํ์ผ๋ก ์ ์ฅํ๊ธฐ
- 1๋จ๊ณ : evelopers.naver.com๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ค์ด๋ฒ ์ผํ '์ ํ์์น' ๊ฒ์์ด ํฌ๋กค๋งํ๊ธฐ
- 2๋จ๊ณ : ํฌ๋กค๋ง ์กฐ๊ฑด์ ์ ์ฌ๋ ๊ธฐ์ค ์์ 1000๊ฐ
- 3๋จ๊ณ : ํฌ๋กค๋งํ ๊ฒฐ๊ณผ๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
- ๋ค์ด๋ฒ api ๊ฒ์ ๊ฐ๋ฐ ๊ฐ์ด๋ ์์ฒญ ๋ณ์
โ๐ป python
# ๋ผ์ด๋ธ๋ฌ๋ฆฌ import import requests import openpyxl # ํ์ ๋ณ์ ์ ์ธ client_id = '๋ด id๊ฐ' client_secret = '๋ด secret๊ฐ' start, num = 1, 0 excel_file = openpyxl.Workbook() excel_sheet = excel_file.active excel_sheet.column_dimensions["B"].width = 100 excel_sheet.column_dimensions["C"].width = 100 excel_sheet.append(['๋ญํฌ', '์ ๋ชฉ', '๋งํฌ']) # ๋งจ์์ ๋ผ๋ฒจ ์ธํ # for๋ฌธ์ ํตํด 1000๊ฐ requests ์์ฒญํ๊ธฐ # display๋ 100๊ฐ์ฉ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ start ์์๊ฐ์ ๋ฐ๊ฟ์ฃผ๋ฉด 1000๊ฐ๋ฅผ ํฌ๋กค๋ง ๊ฐ๋ฅ for index in range(10): start_number = start + (index * 100) naver_open_api = 'https://openapi.naver.com/v1/search/shop.json?query=์ ํ์์น&display=100&start='+str(start_number) # ๋ค์ด๋ฒ ์ผํ์์ ์ ํ์์น ๊ฒ์ํ ์ฃผ์ header_params = {"X-Naver-Client-Id":client_id, "X-Naver-Client-Secret":client_secret} res = requests.get(naver_open_api, headers=header_params) # requests ํ ๊ฒฐ๊ณผ์ ์๋ต์ด ์ ์์ผ ๋์, ์ ์์ด์๋ ๋ if res.status_code == 200: data = res.json() for item in data['items']: # enumerate() ํจ์๋ฅผ ์จ์ ์ธ๋ฑ์ค ๋ฒํธ๋ ํจ๊ป ๋ณผ ์ ์์ num += 1 excel_sheet.append([num, item['title'], item['link']]) # ์ค์ฒฉ list๋ก ์ฒ๋ฆฌํด์ค์ผ ํจ else: print("Error Code: ", res.status_code) # ๋ฌธ์ ์์ผ๋ฉด ์๋ฌ ์ฝ๋ # ํด๋กค๋ง ๊ฒฐ๊ณผ ์ ์ฅ ๋ฐ ๋ซ๊ธฐ excel_file.save('shopping_with_applewatch.xlsx') excel_file.close()
1) ํ๊ฒฝ๊ณต๋จ ๋๊ธฐ์ค์ผ ์ ๋ณด Postman์์ XML ํฌ๋งท ๊ฐ์ ธ์ค๊ธฐ
- ๋๊ธฐ์ค์ผ ์ ๋ณด์กฐํ ์๋น์ค : https://www.data.go.kr/data/15000581/openapi.do
- ํฌ๋กค๋ง ํ์ผ : ์๊ตฐ๊ตฌ๋ณ ์ค์๊ฐ ํ๊ท ์ ๋ณด ์กฐํ
- ์ผ๋ฐ์ธ์ฆํค๋ ์๋น์ค ์ ์ฒญ์ํ๋ฉด ๋ฐ๊ธ ๋ฐ์ ์ ์์
- url์ ?๋ฅผ ๋ถ์ธ ๋ค "์์ฒญ๋ณ์=๊ฐ"์ ์์ฐจ์ ์ผ๋ก ์จ์ฃผ๊ณ ์์ฒญ ๋ณ์ ์ฌ์ด์๋ &๋ฅผ ๋ฃ์ด ์ด์ด์ค
- ์๋น์ค url์ ์์ฒญ๋ณ์์ ํญ๋ชฉ๊ณผ ๊ฐ์ ์์ฑ ํ postman์ผ๋ก ๊ฐ์ ์ ์๋ํ๋์ง๋ง ํ์ธ
- ์๋ ์ฒ๋ผ ํฌ์คํธ๋งจ์ ํ์ธ๋๋ฉด get์ ์ํด์จ ๊ฒ
- XML์ Extensible Markup Language์ ์ฝ์๋ก, ๋ฐ์ดํฐ๋ฅผ ํ๊ทธ๋ก ๊ฐ์ฌ์ ๋งํฌ์ ํ๋ ๋ฒ์ฉ์ ์ธ ํฌ๋งท
Postman
<?xml version="1.0" encoding="UTF-8"?> <response> <header> <resultCode>00</resultCode> <resultMsg>NORMAL SERVICE.</resultMsg> </header> <body> <items> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ฐ๋จ๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.5</coValue> <o3Value>0.022</o3Value> <no2Value>0.029</no2Value> <pm10Value>23</pm10Value> <pm25Value>19</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ฐ๋๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.4</coValue> <o3Value>0.023</o3Value> <no2Value>0.028</no2Value> <pm10Value>30</pm10Value> <pm25Value>21</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ฐ๋ถ๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.5</coValue> <o3Value>0.023</o3Value> <no2Value>0.027</no2Value> <pm10Value>27</pm10Value> <pm25Value>12</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ฐ์๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.4</coValue> <o3Value>0.021</o3Value> <no2Value>0.035</no2Value> <pm10Value>25</pm10Value> <pm25Value>16</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ด์ ๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.5</coValue> <o3Value>0.018</o3Value> <no2Value>0.042</no2Value> <pm10Value>26</pm10Value> <pm25Value>13</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ด์ง๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.5</coValue> <o3Value>0.013</o3Value> <no2Value>0.030</no2Value> <pm10Value>25</pm10Value> <pm25Value>16</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ตฌ๋ก๊ตฌ</cityName> <so2Value>-</so2Value> <coValue>-</coValue> <o3Value>-</o3Value> <no2Value>-</no2Value> <pm10Value>30</pm10Value> <pm25Value>12</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๊ธ์ฒ๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.5</coValue> <o3Value>0.015</o3Value> <no2Value>0.044</no2Value> <pm10Value>38</pm10Value> <pm25Value>24</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๋ ธ์๊ตฌ</cityName> <so2Value>0.004</so2Value> <coValue>0.5</coValue> <o3Value>0.026</o3Value> <no2Value>0.032</no2Value> <pm10Value>31</pm10Value> <pm25Value>16</pm25Value> </item> <item> <dataTime>2021-02-05 19:00</dataTime> <cityName>๋๋ด๊ตฌ</cityName> <so2Value>0.003</so2Value> <coValue>0.3</coValue> <o3Value>0.029</o3Value> <no2Value>0.022</no2Value> <pm10Value>20</pm10Value> <pm25Value>9</pm25Value> </item> </items> <numOfRows>10</numOfRows> <pageNo>1</pageNo> <totalCount>600</totalCount> </body> </response>
2) ํ๊ฒฝ๊ณต๋จ ๋๊ธฐ์ค์ผ ์ ๋ณด vsCode์์ JSON ํฌ๋งท ๊ฐ์ ธ์ค๊ธฐ
- Postman์์ ์ ๊ฐ์ ธ์ค๋์ง ํ์ธํ์์ผ๋ฉด, vsCode์์ requests ์ฃผ์ ๋ณ์๋ก ๋์
- ์๋ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ Postman์์ ๊ฐ์ ธ์จ XML๊ณผ ์ผ์นํจ
- API url์ด ๊ธธ๊ธฐ ๋๋ฌธ์ Service_Key์ ์์ฒญ๋ณ์๋ฅผ ๋ณ์์ ๋ด์ ํ ์ฃผ์์ ์ฐ๊ฒฐ์ํด
โ๐ป pythonimport requests Service_Key = 'API ์ ์ฒญ ํ ๋ฐ์ Key' params = '&numOfRows=10&pageNo=1&sidoName=์์ธ&searchCondition=DAILY' open_api = 'http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnMesureSidoLIst?ServiceKey=' + Service_Key + params res = requests.get(open_api) print(res.text) #Postman์์ ๊ฐ์ ธ์จ XML๊ณผ ์ผ์นํจ
3) XMLํ์์ผ๋ก ๊ฐ์ ธ์จ ๊ณต๊ณต API ํ์ฉํ๊ธฐ
- ์ผ๋ฐ์ ์ผ๋ก JOSN ์ด๋ XML ํ์์ด ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๊ณ ์์
- XML์ CSS์์ฑ์ด ์๋ ํ๊ทธ์ด๋ฏ๋ก find๋ find_all์ ์ด์ฉํด ํ๊ฒํ ํจ
โ๐ป python
import requests from bs4 import BeautifulSoup Service_Key = 'HbJoqYXuPaywwa%2BAWgpMFjmgwbLwqM63jQtmD%2FgLOhWBeMo%2BFas33cpofjKIplNiuh3l4JQdxdDGDzVyBX6hdA%3D%3D' params = '&numOfRows=10&pageNo=1&sidoName=์์ธ&searchCondition=DAILY' open_api = 'http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnMesureSidoLIst?ServiceKey=' + Service_Key + params res = requests.get(open_api) soup = BeautifulSoup(res.content, 'html.parser') # ์ง์ญ, pm์ง์ ์ถ์ถ data = soup.find_all('item') for item in data: cityname = item.find('cityname') pm10value = item.find('pm10value') print(cityname.get_text(), pm10value.get_text())