Skip to content

Latest commit

 

History

History
85 lines (72 loc) · 4.55 KB

ch4.md

File metadata and controls

85 lines (72 loc) · 4.55 KB

4. 使用 API

4-1. API 簡介

API = 網站規定好,(透過程式) 跟它要資料的方式

  • Application Programming Interface
  • 該網站希望你跟它要資料的方法/規則,通常比直接爬網頁方便
  • 方便該網站控管資料流量/使用者驗證 (API key/token...)
  • 與網站溝通的方法 (HTTP Methods):
    • GET: 直接貼上網址(及參數)就能取得資料 ("GET me the infomation")
    • POST: 在背景送出我的資料,希望 Server 能夠處理 ("SUBMIT data to the server")
    • PUT: 在背景送出我想更新的資料 ("UPDATE a record in the DB")
    • DELETE: 在背景送出我想刪除的資料 ("REMOVE a record in the DB")
  • 網站的回應格式
  • 安裝瀏覽器插件 ("JSON/XML Formatter") 或線上轉換可方便觀察資料

4-2. WordPress API 取得部落格文章

4-3. 自由時報新聞網 API

  • ch4/ltn_news_api.py
  • 透過開發者工具,觀察網頁更新時的 XHR (XMLHttpRequest),找尋可能的 API Endpoint
  • API Endpoint: https://news.ltn.com.tw/ajax/breakingnews/popular/[page]
    • 須注意第一頁回傳資料格式與其他頁稍有不同

4-4. IMDB API

  • ch4/imdb_api.py
  • 非官方 API: https://www.omdbapi.com
    • API Endpoint: http://www.omdbapi.com/?apikey=[yourkey]&
    • 先輸入 email 取得 API Key
    • Search by title/ID
    • Search by keywords (使用 page 參數換頁)
    • 中文電影: 先確認英文名字或 imdb id
  • 找出所有 "iron man" 相關影片
    • 用 keywords 搜尋所有相關影片, 記錄其 movie id
    • 用 id 搜尋所有影片, 紀錄相關資訊

4-5. GitHub API: Repositories 列表

4-6. YouTube Data API: 頻道觀看數及影片列表