API と JSON 入門

Pythonによるデータインポート中級

Hugo Bowne-Anderson

Data Scientist at DataCamp

API

  • Application Programming Interface(アプリケーションプログラミングインターフェース)
  • プロトコルとルーティン
    • ソフトウェアアプリケーションの構築と操作

ch_2_1.006.png

Pythonによるデータインポート中級

API

  • Application Programming Interface(アプリケーションプログラミングインターフェース)
  • プロトコルとルーティン
    • ソフトウェアアプリケーションの構築と操作

ch_2_1.007.png

Pythonによるデータインポート中級

JSON

  • JavaScript Object Notation(JavaScriptオブジェクト記法)

  • リアルタイムのサーバー・ブラウザー間通信

  • ダグラス・クロックフォード

  • 人間が読みやすい

ch_2_1.013.png

Pythonによるデータインポート中級

JSON

{'Actors': 'Samuel L. Jackson, Julianna Margulies, Nathan Phillips,
 Rachel Blanchard',
 'Awards': '3 wins & 7 nominations.',
 'Country': 'Germany, USA, Canada',
 'Director': 'David R. Ellis',
 'Genre': 'Action, Adventure, Crime',
 'Language': 'English',
 'Rated': 'R',
 'Released': '18 Aug 2006',
 'Runtime': '105 min',
 'Title': 'Snakes on a Plane',
 'Type': 'movie',
 'Writer': 'John Heffernan (screenplay), Sebastian Gutierrez (screenplay), 
 David Dalessandro (story), John Heffernan (story)',
 'Year': '2006',
 'imdbID': 'tt0417148',
 'imdbRating': '5.6',
 'imdbVotes': '114,668'}
Pythonによるデータインポート中級

JSON

{'Actors': 'Samuel L. Jackson, Julianna Margulies, Nathan Phillips,
 Rachel Blanchard',
 'Awards': '3 wins & 7 nominations.',
 'Country': 'Germany, USA, Canada',
 'Director': 'David R. Ellis',
 'Genre': 'Action, Adventure, Crime',
 'Language': 'English',
 'Rated': 'R',
 'Released': '18 Aug 2006',
 'Runtime': '105 min',
 'Title': 'Snakes on a Plane',    <--
 'Type': 'movie',
 'Writer': 'John Heffernan (screenplay), Sebastian Gutierrez (screenplay), 
 David Dalessandro (story), John Heffernan (story)',
 'Year': '2006',
 'imdbID': 'tt0417148',
 'imdbRating': '5.6',
 'imdbVotes': '114,668'}
Pythonによるデータインポート中級

JSON

{'Actors': 'Samuel L. Jackson, Julianna Margulies, Nathan Phillips,
 Rachel Blanchard',
 'Awards': '3 wins & 7 nominations.',
 'Country': 'Germany, USA, Canada',
 'Director': 'David R. Ellis',
 'Genre': 'Action, Adventure, Crime',
 'Language': 'English',
 'Rated': 'R',
 'Released': '18 Aug 2006',
 'Runtime': '105 min',
 'Title': 'Snakes on a Plane',    <--
 'Type': 'movie',
 'Writer': 'John Heffernan (screenplay), Sebastian Gutierrez (screenplay), 
 David Dalessandro (story), John Heffernan (story)',
 'Year': '2006',                  <--
 'imdbID': 'tt0417148',
 'imdbRating': '5.6',
 'imdbVotes': '114,668'}
Pythonによるデータインポート中級

PythonでのJSONの読み込み

import json
with open('snakes.json', 'r') as json_file: 
       json_data = json.load(json_file)
type(json_data)
dict
Pythonによるデータインポート中級

PythonでのJSONの内容確認

for key, value in json_data.items():
    print(key + ':', value)
Title: Snakes on a Plane
Country: Germany, USA, Canada
Response: True
Language: English
Awards: 3 wins & 7 nominations.
Year: 2006
Actors: Samuel L. Jackson, Julianna Margulies
Runtime: 105 min
Genre: Action, Adventure, Crime
imdbID: tt0417148
Director: David R. Ellis
imdbRating: 5.6
Rated: R
Released: 18 Aug 2006
Pythonによるデータインポート中級

練習しましょう!

Pythonによるデータインポート中級

Preparing Video For Download...