開始使用免費開始

從 API 擷取資料

在上一支影片中,你看到可以透過向 API 發送請求,並剖析 JSON 格式的回應來擷取資料。在這個練習中,你會用 requests 函式庫向 Hacker News API 發送請求,完成相同的流程。

Hacker News 是一個社群新聞彙整網站,主要分享與電腦科學或科技相關的文章。網站上的每則貼文都有對應的 JSON 內容,你會在本練習的請求回應中看到這些 JSON。

本練習屬於課程

Data Engineering 入門

檢視課程

練習說明

  • 使用 requests 模組 get 該 Hacker News 貼文的 JSON 物件。
  • 將回應剖析為 JSON 後列印出來。
  • 再次以 JSON 形式剖析,將貼文中 "score" 鍵的值指定給 post_score

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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)
編輯並執行程式碼