๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ–ช Research

Getting Started with Open API (1)

by soychoi 2024. 7. 22.
728x90
๋ฐ˜์‘ํ˜•
Importing Data from OpenAPI on Public Data Portal to Excel File using Python in Google Colab
๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ ์˜คํ”ˆAPI ๋ฐ์ดํ„ฐ ๊ตฌ๊ธ€์ฝ”๋žฉ ํŒŒ์ด์ฌ ํ†ตํ•ด ์—‘์…€๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•
์™ธ๊ต๋ถ€_๊ตญ๊ฐ€โˆ™์ง€์—ญ๋ณ„ ์ธ๊ตฌ์ฆ๊ฐ ์ •๋ณด

 

 

๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ (data.go.kr)

 

1. ํ•„์š”ํ•œ ์ž๋ฃŒ ๊ฒ€์ƒ‰ ํ›„ Open API "ํ™œ์šฉ์‹ ์ฒญ"ํ•˜์—ฌ ์ธ์ฆํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ €๋Š” ์˜คํ”ˆ API๋กœ ์ €์žฅ๋œ "์™ธ๊ต๋ถ€_๊ตญ๊ฐ€โˆ™์ง€์—ญ๋ณ„ ์ธ๊ตฌ์ฆ๊ฐ ์ •๋ณด"๋ฅผ ์—‘์…€ ํŒŒ์ผ๋กœ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

2. ํ™œ์šฉ์‹ ์ฒญ์„ ํ•˜์—ฌ ์ธ์ฆํ‚ค ๋“ฑ ์—‘์…€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

- End Point (URL)
- ์ผ๋ฐ˜ ์ธ์ฆํ‚ค (Decoding)

 

 

 

(1) ํ™ˆ > ๋งˆ์ดํŽ˜์ด์ง€ > ๋ฐ์ดํ„ฐ ํ™œ์šฉ > Open API > ํ™œ์šฉ์‹ ์ฒญ ํ˜„ํ™ฉ > ๊ฐœ๋ฐœ๊ณ„์ • ์ƒ์„ธ๋ณด๊ธฐ - ์„œ๋น„์Šค ์ •๋ณด - End Point
(2) ๋˜๋Š” ์ฐธ๊ณ ๋ฌธ์„œ์˜ API ์„œ๋น„์Šค ๋ฐฐํฌ์ •๋ณด > ์„œ๋น„์Šค URL ํ†ตํ•ด ๋งํฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

End Point = URL

 

Service key(์„œ๋น„์Šคํ‚ค)๋Š” ์ผ๋ฐ˜ ์ผ์ฆํ‚ค(Decoding)์„ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜น์‹œ ์„œ๋น„์Šคํ‚ค๋ฅผ ์žฌ๋ฐœ๊ธ‰์„ ๋ฐ›์„ ๊ฒฝ์šฐ์—๋Š” "์žฌ๋ฐœ๊ธ‰ ์ธ์ฆํ‚ค"์™€ "์‹ ๊ธ‰๋ฐœ๊ธ‰ ์ธ์ฆํ‚ค"๋ฅผ ํ•จ๊ป˜ ๊ธฐ์ž…ํ•˜๊ฑฐ๋‚˜ "์žฌ๋ฐœ๊ธ‰ ์ธ์ฆํ‚ค"๋งŒ ๊ธฐ์ž…ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 


3. ์ฐธ๊ณ ๋ฌธ์„œ์˜ "Open API ํ™œ์šฉ๊ฐ€์ด๋“œ"๋ฅผ ๋‹ค์šด ๋ฐ›์•„ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„(DataFrame)์— ์‚ฌ์šฉํ•  ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜(Query parameters) ์ •๋ณด๋ฅผ ํš๋“ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

4. ์•„๋ž˜ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌ๊ธ€ ์ฝ”๋žฉ์—์„œ ์ž‘๋™์‹œํ‚ต๋‹ˆ๋‹ค.

 

 
import urllib.request
import json
from pandas import json_normalize
from urllib.parse import urlencode
import pandas as pd
from google.colab import files

# Service URL
api = 'http://apis.data.go.kr/1262000/CountryPopulationService2/getCountryPopulationList2' # End Point ๋˜๋Š” ์„œ๋น„์Šค URL

 

 

๋งŒ์•ฝ "์ผ๋ฐ˜ ์ธ์ฆํ‚ค ์žฌ๋ฐœ๊ธ‰ํ•œ ์ƒํƒœ๋ผ๋ฉด, (1) '์žฌ๋ฐœ๊ธ‰'๋œ ์ผ๋ฐ˜ ์ธ์ฆํ‚ค(Decoding)๋งŒ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ (2) ๋‘ ํ‚ค๋ฅผ ํ•จ๊ป˜ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

 

(1) 

# Service key
service_key = ['RRdu2sm4lP7tX+3YDmpkSRj9Sy1BEyChV+h64EQYYedyvS+163PkCfupS+FWJYSX447gYoadn4Xxq0s50F4I2A=='] # ์ผ๋ฐ˜ ์ธ์ฆํ‚ค(Decoding)

 

 

(2) 

# Service keys
service_keys = [
'RRdu2sm4lP7tX+3YDmpkSRj9Sy1BEyChV+h64EQYYedyvS+163PkCfupS+FWJYSX447gYoadn4Xxq0s50F4I2A==', # ์žฌ๋ฐœ๊ธ‰ ์ธ์ฆํ‚ค
'Opv87t5OZt71vaZcn1U+BMh6oN8REiAIoOJ+8z8uV0sfel1fzta8EKsfeCUktff5Xz0GAOOQx6Kpq0AUG3KjSLQ==' # ์‹ ๊ทœ๋ฐœ๊ธ‰ ์ธ์ฆํ‚ค
]

 

 

(1) ์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ์ •ํ•œ ๊ฒฝ์šฐ, ์ดํ›„ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ์ž‘๋™์‹œ์ผœ์ฃผ์‹œ๋ฉด ํŒŒ์ผ์ด ๋‹ค์šด๋กœ๋“œ ๋ฉ๋‹ˆ๋‹ค.

 
# Fetch and save data with error handling
for key in service_key:
try:
# Initialize an empty list to store DataFrames
all_data_frames = []

# Query parameters without country-specific conditions
queryParams = '?' + urlencode({
'serviceKey': key,
'returnType': 'json',
'numOfRows': '1000', # Adjusted to fetch more rows per request
'pageNo': '1'
})

# Construct URL
url = api + queryParams

# Fetch data
response = urllib.request.urlopen(url).read().decode('utf-8')
print("Response content:", response) # Debugging line to check the response content
data = json.loads(response)
print("Parsed JSON:", data) # Debugging line to inspect parsed JSON

# Check if 'data' key is present
if 'data' in data:
items = data['data']
df = json_normalize(items)
all_data_frames.append(df)

# Concatenate all dataframes
all_data = pd.concat(all_data_frames, ignore_index=True)

# Save to Excel
all_data.to_excel('country_population_all.xlsx', index=False)

print("Data has been saved to country_population_all.xlsx")

# Download the saved Excel file
files.download('country_population_all.xlsx')
break # Exit loop after successful fetch
else:
print("Key 'data' not found in the response.")
except urllib.error.HTTPError as e:
print("HTTP Error:", e.code, e.reason)
except urllib.error.URLError as e:
print("URL Error:", e.reason)
except json.JSONDecodeError as e:
print("JSON Decode Error:", e.msg)
except Exception as e:
print("Unexpected Error:", str(e))

print(f"Failed with service key: {key}")

 

๊ฒฐ๊ณผ๋ฌผ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ๋ฌผ ์บก์ฒ˜๋ณธ (แ„‹แ…ฌแ„€แ…ญแ„‡แ…ฎ_แ„€แ…ฎแ†จแ„€แ…ก·แ„Œแ…ตแ„‹แ…งแ†จแ„‡แ…งแ†ฏแ„‹แ…ตแ†ซแ„€แ…ฎแ„Œแ…ณแ†ผแ„€แ…กแ†ทแ„Œแ…ฅแ†ผแ„‡แ…ฉ_23๋…„ ์—…๋กœ๋“œ)

 

 

 

์ „์ฒด์ฝ”๋“œ

 

import urllib.request
import json
from pandas import json_normalize
from urllib.parse import urlencode
import pandas as pd
from google.colab import files

# Service URL
api = 'http://apis.data.go.kr/1262000/CountryPopulationService2/getCountryPopulationList2' # End Point ๋˜๋Š” ์„œ๋น„์Šค URL

# Service keys
service_key = ['RRdu2sm4lP7tX+3YDmpkSRj9Sy1BEyChV+h64EQYYedyvS+163PkCfupS+FWJYSX447gYoadn4Xxq0s50F4I2A=='] # ์ผ๋ฐ˜ ์ธ์ฆํ‚ค(Decoding)

# Fetch and save data with error handling
for key in service_key:
try:
# Initialize an empty list to store DataFrames
all_data_frames = []

# Query parameters without country-specific conditions
queryParams = '?' + urlencode({
'serviceKey': key,
'returnType': 'json',
'numOfRows': '1000', # Adjusted to fetch more rows per request
'pageNo': '1'
})

# Construct URL
url = api + queryParams

# Fetch data
response = urllib.request.urlopen(url).read().decode('utf-8')
print("Response content:", response) # Debugging line to check the response content
data = json.loads(response)
print("Parsed JSON:", data) # Debugging line to inspect parsed JSON

# Check if 'data' key is present
if 'data' in data:
items = data['data']
df = json_normalize(items)
all_data_frames.append(df)

# Concatenate all dataframes
all_data = pd.concat(all_data_frames, ignore_index=True)

# Save to Excel
all_data.to_excel('country_population_all.xlsx', index=False)

print("Data has been saved to country_population_all.xlsx")

# Download the saved Excel file
files.download('country_population_all.xlsx')
break # Exit loop after successful fetch
else:
print("Key 'data' not found in the response.")
except urllib.error.HTTPError as e:
print("HTTP Error:", e.code, e.reason)
except urllib.error.URLError as e:
print("URL Error:", e.reason)
except json.JSONDecodeError as e:
print("JSON Decode Error:", e.msg)
except Exception as e:
print("Unexpected Error:", str(e))

print(f"Failed with service key: {key}")

 

์ฐธ๊ณ ๋กœ ํ•ด๋‹น ์ฝ”๋“œ ์ž‘๋™์‹œ ๋ฐ›๋Š” ์‘๋‹ต์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

 

728x90
๋ฐ˜์‘ํ˜•