API 与万维网交互

Python 数据导入进阶

Hugo Bowne-Anderson

Data Scientist at DataCamp

本章将学习

  • 什么是 API
  • API 为何重要
  • 练习中将:
    • 连接到 API
    • 从 API 拉取数据
    • 解析 API 数据
Python 数据导入进阶

什么是 API?

  • 一组协议与例程
  • 一堆代码
    • 使两个软件程序彼此通信

ch_2_2.013.png

Python 数据导入进阶

什么是 API?

  • 一组协议与例程
  • 一堆代码
    • 使两个软件程序彼此通信

ch_2_2.014.png

Python 数据导入进阶

API 无处不在

ch_2_2.016.png

Python 数据导入进阶

API 无处不在

ch_2_2.017.png

Python 数据导入进阶

API 无处不在

ch_2_2.018.png

Python 数据导入进阶

API 无处不在

ch_2_2.019.png

Python 数据导入进阶

在 Python 中连接到 API

import requests
url = 'http://www.omdbapi.com/?t=hackers'
r = requests.get(url)
json_data = r.json()
for key, value in json_data.items():
    print(key + ':', value)
Python 数据导入进阶

这个 URL 是什么?

  • http —— 发起 HTTP 请求
  • www.omdbapi.com —— 查询 OMDB API
  • ?t=hackers

    • 查询字符串
    • 返回标题 (t) 为 "Hackers" 的电影数据

    'http://www.omdbapi.com/?t=hackers'

Python 数据导入进阶

OMDb API

ch_2_2.033.png

Python 数据导入进阶

OMDb API

ch_2_2.034.png

Python 数据导入进阶

这就是普通 URL!

ch_2_2.036.png

Python 数据导入进阶

开始练习吧!

Python 数据导入进阶

Preparing Video For Download...