BaşlayınÜcretsiz Başlayın

API requests with urllib

For this course, you will be using the API for a Music Catalog application. This API has multiple features. You will start with the Lyrics API, which allows you to retrieve a quote from the Lyric of the day.

Before you can make your first API request, you will need to know where the API can be accessed. This location is also referred to as the URL, short for Uniform Resource Locator. The URL will tell Python where to send the API request to. The URL for the Lyrics API is as follows: http://localhost:3000/lyrics/.

Let's make a first request to the Lyrics API using the built-in urllib Python module.

Bu egzersiz

Introduction to APIs in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Use the read function on the response object to read the response data from the response object.
  • Use the decode function to decode the response data into a string with the right encoding.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

from urllib.request import urlopen

with urlopen('http://localhost:3000/lyrics/') as response:
  
  # Use the correct function to read the response data from the response object
  data = response.____()
  encoding = response.headers.get_content_charset()

  # Decode the response data so you can print it as a string later
  string = data.____(encoding)
  
  print(string)
Kodu Düzenle ve Çalıştır