CodeGym /Khóa học Java /Python SELF VI /Làm việc với API công khai

Làm việc với API công khai

Python SELF VI
Mức độ , Bài học
Có sẵn

9.1 Làm việc với Google Maps API

Cùng làm việc một chút với API công khai của các dịch vụ phổ biến nhé.

Ví dụ, Google Maps API cung cấp các dịch vụ như geocoding, tìm đường và vị trí. Để sử dụng Google Maps API cần đăng ký API key.

Geocoding (nhận tọa độ từ địa chỉ)


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'Tọa độ: {lat}, {lng}')
else:
    print('Lỗi geocoding')

9.2 Làm việc với OpenWeatherMap API

Một ví dụ tuyệt vời khác là lấy thời tiết tại bất kỳ điểm nào trên thế giới.

Dịch vụ OpenWeatherMap API cung cấp dữ liệu thời tiết trên toàn thế giới. Để sử dụng API cần đăng ký và nhận API key.

Nhận thời tiết hiện tại


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'Thời tiết ở {city}: {weather}, nhiệt độ: {temp}°C')
else:
    print('Lỗi nhận dữ liệu thời tiết')

9.3 Làm việc với GitHub API

Các bạn đều là lập trình viên, hãy làm gì đó liên quan đến IT hơn đi. Ví dụ, có thể khám phá các repository trên GitHub.

GitHub API cung cấp truy cập thông tin về repository, người dùng và tổ chức.

Nhận thông tin về repository


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"Repository: {data['name']}")
    print(f"Mô tả: {data['description']}")
    print(f"Số sao: {data['stargazers_count']}")
else:
    print('Lỗi nhận thông tin về repository')

9.4 Làm việc với YouTube Data API

YouTube Data API cho phép nhận thông tin về video, kênh và danh sách phát. Để sử dụng API cần có API key.


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'Tiêu đề video: {title}')
    print(f'Lượt xem: {views}')
else:
    print('Lỗi nhận thông tin về video')

Open Notify API cung cấp dữ liệu về vị trí hiện tại của Trạm vũ trụ quốc tế (ISS).

Nhận vị trí hiện tại của 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 đang ở tọa độ: kinh độ {position['longitude']}, vĩ độ {position['latitude']}")
else:
    print('Lỗi nhận dữ liệu')
Bình luận
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION