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

Using the requests package

Using urllib to integrate APIs can result in verbose and complex code as you need to take care of a lot of additional things like encoding and decoding responses.

As an alternative to urllib, the requests Python package offers a simpler way to integrate APIs. A lot of functionality is available out of the box with requests, which makes your code a lot easier to write and read. Let's try the same exercise again but now with the requests package.

Remember, as with the previous exercise, the URL for the Lyrics API is http://localhost:3000/lyrics.

Bu egzersiz

Introduction to APIs in Python

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

Egzersiz talimatları

  • Import the requests package.
  • Pass the URL http://localhost:3000/lyrics to the requests.get method.
  • Print out the response text.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Import the requests package
import ____

# Pass the API URL to the get function
response = requests.get(____)

# Print out the text attribute of the response object
print(response.____)
Kodu Düzenle ve Çalıştır