Aan de slagGa gratis aan de slag

Fetch from an API

In the last video, you've seen that you can extract data from an API by sending a request to the API and parsing the response which was in JSON format. In this exercise, you'll be doing the same by using the requests library to send a request to the Hacker News API.

Hacker News is a social news aggregation website, specifically for articles related to computer science or the tech world in general. Each post on the website has a JSON representation, which you'll see in the response of the request in the exercise.

Deze oefening maakt deel uit van de cursus

Introduction to Data Engineering

Cursus bekijken

Oefeninstructies

  • Use the requests module to get the Hacker News post's JSON object.
  • Print out the response, parsed as a JSON.
  • Parsing as JSON again, assign the "score" key of the post to post_score.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

import requests

# Fetch the Hackernews post
resp = requests.____("https://hacker-news.firebaseio.com/v0/item/16222426.json")

# Print the response parsed as JSON
print(resp.____())

# Assign the score of the test to post_score
post_score = resp.___()["____"]
print(post_score)
Code bewerken en uitvoeren