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('๋ฐ์ดํฐ ์ป๊ธฐ ์ค๋ฅ')
GO TO FULL VERSION