บทนำสู่ API

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Amany Mahfouz

Instructor

Application Programming Interfaces

  • กำหนดวิธีที่แอปพลิเคชันสื่อสารกับโปรแกรมอื่น
  • ดึงข้อมูลจากแอปพลิเคชันโดยไม่ต้องรู้รายละเอียดฐานข้อมูล

แบบฟอร์มที่มีบางช่องกรอกและบางช่องว่าง

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Application Programming Interfaces

  • กำหนดวิธีที่แอปพลิเคชันสื่อสารกับโปรแกรมอื่น
  • ดึงข้อมูลจากแอปพลิเคชันโดยไม่ต้องรู้รายละเอียดฐานข้อมูล

แบบฟอร์มและหน้าร้านค้า มีลูกศรแสดงว่าแบบฟอร์มกำลังถูกส่งไปยังร้านค้า

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Application Programming Interfaces

  • กำหนดวิธีที่แอปพลิเคชันสื่อสารกับโปรแกรมอื่น
  • ดึงข้อมูลจากแอปพลิเคชันโดยไม่ต้องรู้รายละเอียดฐานข้อมูล

กล่องพัสดุที่เปิดอยู่และหน้าร้านค้า กล่องพัสดุอยู่ในตำแหน่งที่เคยเป็นแบบฟอร์มสั่งซื้อ มีลูกศรแสดงว่าร้านค้าได้ส่งพัสดุออกไป

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Requests

  • ส่งและรับข้อมูลจากเว็บไซต์
  • ไม่ผูกกับ API ใด API หนึ่งเป็นพิเศษ
  • requests.get() สำหรับดึงข้อมูลจาก URL

โลโก้ requests

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

requests.get()

  • requests.get(url_string) สำหรับดึงข้อมูลจาก URL
  • Keyword arguments
    • คีย์เวิร์ด params: รับ dictionary ของพารามิเตอร์และค่าต่าง ๆ เพื่อปรับแต่งคำขอ API
    • คีย์เวิร์ด headers: รับ dictionary ใช้สำหรับยืนยันตัวตนกับ API
  • ผลลัพธ์: อ็อบเจกต์ response ที่มีข้อมูลและ metadata
    • response.json() จะคืนค่าเฉพาะข้อมูล JSON
การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

response.json() และ pandas

  • response.json() คืนค่าเป็น dictionary
  • read_json() รับค่าเป็น string เท่านั้น ไม่ใช่ dictionary
  • โหลด JSON จาก response ไปยัง dataframe ด้วย pd.DataFrame()
    • read_json() จะเกิดข้อผิดพลาด!
การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Yelp Business Search API

โลโก้ Yelp

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Yelp Business Search API

เอกสาร Yelp Business Search API แสดง endpoint ของคำขอและพารามิเตอร์บางส่วน

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Yelp Business Search API

เอกสาร Yelp Business Search API โดยไฮไลต์ URL ของ API endpoint

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Yelp Business Search API

เอกสาร Yelp Business Search API โดยไฮไลต์พารามิเตอร์ term ที่เป็นตัวเลือก

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Yelp Business Search API

เอกสาร Yelp Business Search API โดยไฮไลต์พารามิเตอร์ตำแหน่งที่จำเป็น

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Yelp Business Search API

เอกสาร Yelp Business Search API แสดงตัวอย่าง response JSON

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

การส่ง Request

import requests
import pandas as pd

api_url = "https://api.yelp.com/v3/businesses/search"
# Set up parameter dictionary according to documentation params = {"term": "bookstore", "location": "San Francisco"}
# Set up header dictionary w/ API key according to documentation headers = {"Authorization": "Bearer {}".format(api_key)}
# Call the API response = requests.get(api_url, params=params, headers=headers)
การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

การแปลงผลลัพธ์จาก Response

# Isolate the JSON data from the response object
data = response.json()
print(data)
{'businesses': [{'id': '_rbF2ooLcMRA7Kh8neIr4g', 'alias': 'city-lights-bookstore-san-francisco', 'name': 'City Lights Bookstore', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/VRydkkpVbA3CeVLBKzs2Vw/o.jpg', 'is_closed': False,
# Load businesses data to a dataframe
bookstores = pd.DataFrame(data["businesses"])
print(bookstores.head(2))
                                  alias        ...                                                    url
0   city-lights-bookstore-san-francisco        ...      https://www.yelp.com/biz/city-lights-bookstore...
1  alexander-book-company-san-francisco        ...      https://www.yelp.com/biz/alexander-book-compan...

[2 rows x 16 columns]
การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

มาฝึกกันเถอะ!

การนำเข้าข้อมูลด้วย pandas อย่างมีประสิทธิภาพ

Preparing Video For Download...