CodeGym /์ž๋ฐ” ์ฝ”์Šค /Python SELF KO /๊ณต๊ฐœ API ์ž‘์—…

๊ณต๊ฐœ API ์ž‘์—…

Python SELF KO
๋ ˆ๋ฒจ 24 , ๋ ˆ์Šจ 3
์‚ฌ์šฉ ๊ฐ€๋Šฅ

9.1 Google Maps API ์ž‘์—…

์ธ๊ธฐ ์„œ๋น„์Šค์˜ ๊ณต๊ฐœ API๋ฅผ ์กฐ๊ธˆ ๋‹ค๋ค„๋ณด์ž.

์˜ˆ๋ฅผ ๋“ค์–ด, Google Maps API๋Š” ์ง€์˜ค์ฝ”๋”ฉ, ๊ฒฝ๋กœ ์ฐพ๊ธฐ, ์œ„์น˜ ์ •๋ณด ๋“ฑ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ด. Google Maps API๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด API ํ‚ค๋ฅผ ๋“ฑ๋กํ•ด์•ผ ํ•ด.

์ง€์˜ค์ฝ”๋”ฉ (์ฃผ์†Œ๋กœ ์ขŒํ‘œ ์–ป๊ธฐ)


import requests
API_KEY = 'YOUR_GOOGLE_MAPS_API_KEY'
address = '1600 Amphitheatre Parkway, Mountain View, CA'
url = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={API_KEY}'
response = requests.get(url)
data = response.json()
if data['status'] == 'OK':
    location = data['results'][0]['geometry']['location']
    lat = location['lat']
    lng = location['lng']
    print(f'์ขŒํ‘œ: {lat}, {lng}')
else:
    print('์ง€์˜ค์ฝ”๋”ฉ ์˜ค๋ฅ˜')

9.2 OpenWeatherMap API ์ž‘์—…

๋˜ ๋‹ค๋ฅธ ๋ฉ‹์ง„ ์˜ˆ๋Š” ์ „ ์„ธ๊ณ„ ์–ด๋–ค ๊ณณ์˜ ๋‚ ์”จ๋ฅผ ์–ป๋Š” ๊ฑฐ์•ผ.

OpenWeatherMap API ์„œ๋น„์Šค๋Š” ์ „ ์„ธ๊ณ„์˜ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ด. API๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋“ฑ๋กํ•˜๊ณ  API ํ‚ค๋ฅผ ๋ฐ›์•„์•ผ ํ•ด.

ํ˜„์žฌ ๋‚ ์”จ ์–ป๊ธฐ


import requests
API_KEY = 'YOUR_OPENWEATHERMAP_API_KEY'
city = 'London'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
    weather = data['weather'][0]['description']
    temp = data['main']['temp']
    print(f'{city}์˜ ๋‚ ์”จ: {weather}, ์˜จ๋„: {temp}ยฐC')
else:
    print('๋‚ ์”จ ๋ฐ์ดํ„ฐ ์–ป๊ธฐ ์˜ค๋ฅ˜')

9.3 GitHub API ์ž‘์—…

๋„ˆ๋„ค ํ”„๋กœ๊ทธ๋ž˜๋จธ์ž–์•„, ์ข€ ๋” ๊ฐœ๋ฐœ์ž์Šค๋Ÿฌ์šด ๊ฑฐ ํ•ด๋ณด์ž. ์˜ˆ๋ฅผ ๋“ค์–ด, GitHub ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฑฐ์ง€.

GitHub API๋Š” ๋ ˆํฌ์ง€ํ† ๋ฆฌ, ์‚ฌ์šฉ์ž, ์กฐ์ง์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด.

๋ ˆํฌ์ง€ํ† ๋ฆฌ ์ •๋ณด ์–ป๊ธฐ


import requests
repo_owner = 'octocat'
repo_name = 'Hello-World'
url = f'https://api.github.com/repos/{repo_owner}/{repo_name}'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
    print(f"๋ ˆํฌ์ง€ํ† ๋ฆฌ: {data['name']}")
    print(f"์„ค๋ช…: {data['description']}")
    print(f"์Šคํƒ€: {data['stargazers_count']}")
else:
    print('๋ ˆํฌ์ง€ํ† ๋ฆฌ ์ •๋ณด ์–ป๊ธฐ ์˜ค๋ฅ˜')

9.4 YouTube Data API ์ž‘์—…

YouTube Data API๋Š” ๋น„๋””์˜ค, ์ฑ„๋„, ์žฌ์ƒ ๋ชฉ๋ก์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค˜. API๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด API ํ‚ค๋ฅผ ๋ฐ›์•„์•ผ ํ•ด.


import requests
API_KEY = 'YOUR_YOUTUBE_API_KEY'
video_id = 'Ks-_Mh1QhMc'
url = f'https://www.googleapis.com/youtube/v3/videos?id={video_id}&key={API_KEY}&part=snippet,contentDetails,statistics'
response = requests.get(url)
data = response.json()
if 'items' in data and len(data['items']) > 0:
    video_info = data['items'][0]
    title = video_info['snippet']['title']
    views = video_info['statistics']['viewCount']
    print(f'๋น„๋””์˜ค ์ œ๋ชฉ: {title}')
    print(f'์กฐํšŒ์ˆ˜: {views}')
else:
    print('๋น„๋””์˜ค ์ •๋ณด ์–ป๊ธฐ ์˜ค๋ฅ˜')

Open Notify API๋Š” ๊ตญ์ œ ์šฐ์ฃผ ์ •๊ฑฐ์žฅ(ISS)์˜ ํ˜„์žฌ ์œ„์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ด.

ISS์˜ ํ˜„์žฌ ์œ„์น˜ ์–ป๊ธฐ


import requests
url = 'http://api.open-notify.org/iss-now.json'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
    position = data['iss_position']
    print(f"ISS๋Š” ํ˜„์žฌ ์œ„์น˜: ๊ฒฝ๋„ {position['longitude']}, ์œ„๋„ {position['latitude']}")
else:
    print('๋ฐ์ดํ„ฐ ์–ป๊ธฐ ์˜ค๋ฅ˜')
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION