๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

[API] ์„ธ๊ณ„ ๊ฐ๊ตญ์˜ ๊ธฐ๋ณธ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ (restcountries)

by nowgeun 2023. 2. 6.
728x90

์ง๊ตฌ๊ฐ€ ํ•„์š”ํ•œ ๋ฌผ๊ฑด์ด ์žˆ์–ด์„œ ํ•ด์™ธ์‚ฌ์ดํŠธ์— ํšŒ์›๊ฐ€์ž…์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์ž… ์ง„ํ–‰ ์ ˆ์ฐจ์— ๊ตญ๊ฐ€๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋Š”๋ฐ, ๋ฌธ๋“ ๊ฐœ๋ฐœ์ž๋“ค์€ ์ด๋Ÿฐ ์ •๋ณด๋ฅผ ์–ด๋””์„œ ๊ฐ€์ ธ์˜ค๋Š”์ง€๊ฐ€ ๊ถ๊ธˆํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๋˜ ๊ถ๊ธˆํ•œ ๊ฒƒ์€ ๋ชป ์ฐธ๋Š” ์„ฑ๊ฒฉ์ด๋ผ ๋ฐ”๋กœ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๊ณ , restcountries๋ผ๋Š” API๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฌด๋ฃŒ ์‚ฌ์ดํŠธ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค:

https://restcountries.com/#api-endpoints-v2-list-of-codes

์ด 250๊ฐœ ๊ตญ๊ฐ€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ REST API๋กœ ์ œ๊ณต

 

๊ฐ๊ตญ์˜ ๋ช…์นญ, ๊ตญ๊ฐ€์ฝ”๋“œ, ํ†ตํ™”, ์œ„์„ฑ์ขŒํ‘œ, ์‚ฌ์šฉ ์–ธ์–ด ๋“ฑ์˜ ๊ตญ๊ฐ€์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” API๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•œ๋ฒˆ ํƒ์ƒ‰์„ ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. (๋ณ„๋„์˜ API Key๋ฅผ ๋ฐœ๊ธ‰ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค)

 

๋Œ€ํ•œ๋ฏผ๊ตญ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•œ๊ตญ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋Œ€ํ•œ๋ฏผ๊ตญ๊ณผ ๋ถํ•œ์˜ ์ •๋ณด๋ฅผ ๋‘˜๋‹ค ๊ฐ€์ ธ์™€์„œ 'South Korea' ํ˜น์€ 'Republic of Korea'๋ผ๊ณ  ๊ฒ€์ƒ‰์„ ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. Native name๋„ ์ง€์›์„ ํ•œ๋‹ค๊ณ  ํ•ด์„œ '๋Œ€ํ•œ๋ฏผ๊ตญ', 'ํ•œ๊ตญ'์œผ๋กœ ๊ฒ€์ƒ‰๋„ ํ•ด๋ดค๋Š”๋ฐ 404 ์—๋Ÿฌ๊ฐ€ ๋œจ๋Š” ๊ฑธ ๋ณด๋‹ˆ ๊ฒ€์ƒ‰์ด ์™„๋ฒฝํ•˜์ง€๋Š” ์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

import requests

country = 'korea'
url = f'https://restcountries.com/v3.1/name/{country}'
req = requests.get(url)
json_data = req.json()

REST API๋กœ ๊ฐ€์ ธ์˜จ ๋Œ€ํ•œ๋ฏผ๊ตญ json ๋ฐ์ดํ„ฐ

 

๊ฐ€์ ธ์˜จ ์ •๋ณด๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ๋ฐฉ๋Œ€ํ•ด์„œ ๋†€๋ž์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ œ๊ณตํ•˜๋Š” ํ•ญ๋ชฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

dict_keys(['name', 'tld', 'cca2', 'ccn3', 'cca3', 'cioc', 'independent', 'status', 'unMember', 'currencies', 'idd', 'capital', 'altSpellings', 'region', 'subregion', 'languages', 'translations', 'latlng', 'landlocked', 'borders', 'area', 'demonyms', 'flag', 'maps', 'population', 'gini', 'fifa', 'car', 'timezones', 'continents', 'flags', 'coatOfArms', 'startOfWeek', 'capitalInfo', 'postalCode'])

์ธ๊ตฌ, ์ˆ˜๋„, ํ†ตํ™”, ๊ตญ๊ฐ€ ์ฝ”๋“œ, ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ๋“ฑ ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์–ด์„œ ๊ตญ์ œ์‚ฌํšŒ ์—ฐ๊ตฌ๋ฅผ ํ•˜๋Š” ๋ถ„๋“ค์—๊ฒŒ๋Š” ์•„์ฃผ ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

250๊ฐœ๊ตญ์˜ ์ˆ˜๋„ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ

ํ€ด์ฆˆํ•˜๋ฉด ๋น ์งˆ ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด ๊ตญ๊ฐ€์˜ ์ˆ˜๋„์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š”  restcountries์˜ API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ๊ตญ์˜ ์ˆ˜๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import requests
import pandas as pd

url = f'https://restcountries.com/v3.1/all'
req = requests.get(url)
json_data = req.json()

def extract_capital(x):
    if x:
        return x[0]
    else:
        return None

    
def extract_coord(x):
    if x.keys():
        return x['latlng']
    else:
        return [None,None]


# json ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ
capital_df = pd.DataFrame(json_data)[['name','capital','capitalInfo']]
capital_df = capital_df.fillna("")
capital_df['country'] = capital_df['name'].apply(lambda x: x['common'])
capital_df['capital'] = capital_df['capital'].apply(lambda x: extract_capital(x))
capital_df['coord'] = capital_df['capitalInfo'].apply(lambda x: extract_coord(x))
capital_df = capital_df[['country','capital','coord']]

capital_df

๋ฐ์ดํ„ฐ ์กฐํšŒ ๊ฒฐ๊ณผ

๊ฐ ๊ตญ๊ฐ€์˜ ์ˆ˜๋„ ์ด๋ฆ„๊ณผ ์ขŒํ‘œ๊นŒ์ง€ ์ž˜ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ •ํ•ฉ์„ฑ์„ ์œ„ํ•ด ๊ตฌ๊ธ€ ๋งต์„ ์ด์šฉ ํ•˜์—ฌ ์ขŒํ‘œ๋ฅผ ์ฐ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งจ ๋งˆ์ง€๋ง‰์— ์กฐํšŒ๋˜๋Š” ์ž๋ฉ”์ด์นด์˜ ์œ„๋„์™€ ๊ฒฝ๋„ ์ •๋ณด๋ฅผ ๊ตฌ๊ธ€๋งต์— ์ž…๋ ฅ์„ ํ–ˆ๊ณ , ํ‚น์Šคํ„ด์˜ ์œ„์น˜๊ฐ€ ์ž˜ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค:

๋ฐ์ดํ„ฐ ์ •ํ•ฉ์„ฑ Google Map์œผ๋กœ ์ฒดํฌ

 

 

250๊ฐœ๊ตญ์˜ ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ

๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ณธ ๊ฒฐ๊ณผ ์ด 250๊ฐœ์˜ ๊ตญ๊ฐ€์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ฐ ๊ตญ๊ฐ€์˜ ๊ตญ๊ธฐ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์™€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

import requests
import os
from PIL import Image
from io import BytesIO


url = f'https://restcountries.com/v3.1/all'
req = requests.get(url)
json_data = req.json()

# ๊ตญ๊ฐ€ ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ
country_data = [c['name']['common'] for c in json_data]

# ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ํŒŒ์ผ URL ๊ฐ€์ ธ์˜ค๊ธฐ
flag_png_url_data = [c['flags']['png'] for c in json_data]

# ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ €์žฅ ๋””๋ ‰ํ† ๋ฆฌ ์ง€์ •
save_dir = '/Users/jake1/Desktop/flag_imgs/'

# ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
def get_png_files(country,url, save_dir):
    png_file = requests.get(url).content
    im = Image.open(BytesIO(png_file))
    im.save(save_dir + country + '.png')
    return

# 250๊ฐœ ๊ตญ๊ฐ€์˜ ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์‹คํ–‰
for country, url in zip(country_data, flag_png_url_data):
    get_png_files(country,url,save_dir)

# ์ €์žฅ๋œ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฐœ์ˆ˜ ํ™•์ธ
print(len([f for f in os.listdir(save_dir) if f.endswith('.png')]))

# ์บ๋‚˜๋‹ค ๊ตญ๊ธฐ ๊ฐ€์ ธ์˜ค๊ธฐ (Ipynb ๋…ธํŠธ๋ถ์—์„œ๋งŒ ์ž‘๋™)
canada_flag = save_dir + 'Canada.png'
im = Image.open(canada_flag)
display(im)

Canada Flag Image
์ฝ”๋“œ ๊ฒฐ๊ณผ์ฐฝ

์บ๋‚˜๋‹ค ๊ตญ๊ธฐ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์™€์กŒ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ save_dir๋กœ ์ง€์ •ํ•œ ํด๋” ๋‚ด ์ด๋ฏธ์ง€๋“ค์ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ €์žฅ์ด ์ž˜ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ์จ ์ผ์ผ์ด ๊ตฌ๊ธ€์— ๊ฐ ๊ตญ๊ฐ€๋ณ„๋กœ ๊ตญ๊ธฐ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ง‘ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ํด๋”์— ๋‹ค์šด๋กœ๋“œ ๋œ ๊ตญ๊ธฐ ์ด๋ฏธ์ง€ ์ผ๋ถ€ ๋ชฉ๋ก


๋งˆ๋ฌด๋ฆฌ

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ฐ ๊ตญ๊ฐ€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” rest countries์˜ API๋ฅผ ํ™œ์šฉํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์šด์˜์„ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๋งŽ์€ ํŠธ๋ž˜ํ”ฝ ์š”์ฒญ์„ ๋ณ„๋‹ค๋ฅธ ์ˆ˜์ต ์—†์ด ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ๋“œ๋Š” ๋น„์šฉ์ด ์ƒ๋‹นํ•  ๊ฒƒ์œผ๋กœ ์ถ”์ •๋ฉ๋‹ˆ๋‹ค. ์—ฌ์œ ๊ฐ€ ์žˆ์œผ์‹  ๋ถ„๋“ค์€ ์ปคํ”ผ ํ•œ์ž” ๊ฐ’์ด๋ผ๋„ ๋ณด๋‚ด์ฃผ์‹œ๋ฉด ๊ฐœ๋ฐœ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ํฐ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

IT ์—…๊ณ„๊ฐ€ ๋น ๋ฅธ ์†๋„๋กœ ๋ฐœ์ „์„ ํ•œ ๊ฒƒ๋„ ์˜คํ”ˆ์†Œ์Šค์™€ ๋ฌด๋ฃŒ API๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ›—๋‚  ์ €๋„ ์„œ๋ฒ„๋ฅผ ํ•˜๋‚˜ ์šด์˜ํ•ด์„œ ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ API๋กœ ์ œ๊ณตํ•ด๋ณด๊ณ  ์‹ถ์–ด ์กŒ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์˜ค๋Š˜์€ ์—ฌ๊ธฐ์„œ ๋งˆ๋ฌด๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ž˜ ๋ณด์…จ๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ ๊ณต๊ฐ ๊พน~ ๋ˆŒ๋Ÿฌ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€