API,即 应用程序编程接口 ,促进了两个软件之间的通信。它允许您使用代码检索和发送数据。我们最常使用 API 来检索数据,这将是本初学者友好教程的重点。
当我们想要从 API 接收数据时,我们需要发出请求 。 请求在网络上随处可见。例如,当您访问此博客文章时,您的网络浏览器会向 网络服务器发出请求,该服务器会响应此网页的内容。
API 请求的工作方式完全相同 - 您向 API 服务器发出数据请求,然后它会响应您的请求。
不同的 HTTP 方法和状态代码
REST API 有多种 HTTP 方法 。这些方法告诉 API 需要对数据执行哪些操作。虽然 HTTP 方法有很多,但下面列出的五种方法是 REST API 最常用的:
HTTP 方法 | 描述 |
---|---|
GET
|
检索现有数据 |
POST
|
添加新数据 |
PUT
|
更新现有数据 |
PATCH
|
部分更新现有数据 |
DELETE
|
删除数据 |
REST API 收到并处理 HTTP 请求后,会返回带有 HTTP 状态代码的响应。此状态代码提供有关响应的信息,并帮助客户端应用程序了解响应的类型。
状态代码根据结果的类别进行编号:
代码范围 | 类别 |
---|---|
1xx
|
信息响应 |
2xx
|
成功操作 |
3xx
|
重定向 |
4xx
|
客户端错误 |
5xx
|
服务器错误 |
MDN Web 文档 中了解有关 HTTP 状态代码的更多信息 .
API 端点
API 端点是服务器公开的公共 URL,客户端应用程序使用它来访问资源和数据。
为了本教程的目的,我们将使用 Fake Store REST API 。更具体地说,我们将使用以下端点:
HTTP 方法 | API 端点 | 描述 |
---|---|---|
GET
|
/products
|
获取产品列表。 |
GET
|
/products?limit=x
|
仅获得 5 件产品。 |
GET
|
/products/<product_id>
|
获取单品。 |
POST
|
/products
|
创建新产品。 |
PUT
|
/products/<product_id>
|
更新产品。 |
PATCH
|
/products/<product_id>
|
部分更新产品。 |
DELETE
|
/products/<product_id>
|
删除商品。 |
上述每个端点根据 HTTP 方法执行不同的操作。对于每个 API URL,基本 URL 为: https://fakestoreapi.com
。我们将逐一探索它们。
但首先我们需要安装一个外部库来使用这些 API。大多数 Python 开发人员使用该 requests
库与 Web 服务进行交互。您可以使用 pip
以下命令安装此库:
$ pip install requests
一旦安装了库,我们就可以开始了!
如何发出 GET 请求
这是您遇到的最常见的 HTTP 请求方法之一。它是一种 只读 操作,允许您从 API 中检索数据。
让我们在上面提到的第一个端点上尝试一下 GET 请求,它会以产品列表作为响应。
import requests
BASE_URL = 'https://fakestoreapi.com'
response = requests.get(f"{BASE_URL}/products")
print(response.json())
上述脚本使用 requests.get()
方法向 API 端点发送 GET 请求 /products
。它以所有产品的列表作为响应。然后我们调用 .json()
以查看 JSON 响应,如下所示:
[
{
"id": 1,
"title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
"rating": {
"rate": 3.9,
"count": 120
}
},
{
"id": 2,
"title": "Mens Casual Premium Slim Fit T-Shirts ",
"price": 22.3,
"description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg",
"rating": {
"rate": 4.1,
"count": 259
}
},
{
"id": 3,
"title": "Mens Cotton Jacket",
"price": 55.99,
"description": "great outerwear jackets for Spring/Autumn/Winter, suitable for many occasions, such as working, hiking, camping, mountain/rock climbing, cycling, traveling or other outdoors. Good gift choice for you or your family member. A warm hearted love to Father, husband or son in this thanksgiving or Christmas Day.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71li-ujtlUL._AC_UX679_.jpg",
"rating": {
"rate": 4.7,
"count": 500
}
},
{
"id": 4,
"title": "Mens Casual Slim Fit",
"price": 15.99,
"description": "The color could be slightly different between on the screen and in practice. / Please note that body builds vary by person, therefore, detailed size information should be reviewed below on the product description.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71YXzeOuslL._AC_UY879_.jpg",
"rating": {
"rate": 2.1,
"count": 430
}
},
{
"id": 5,
"title": "John Hardy Women's Legends Naga Gold & Silver Dragon Station Chain Bracelet",
"price": 695,
"description": "From our Legends Collection, the Naga was inspired by the mythical water dragon that protects the ocean's pearl. Wear facing inward to be bestowed with love and abundance, or outward for protection.",
"category": "jewelery",
"image": "https://fakestoreapi.com/img/71pWzhdJNwL._AC_UL640_QL65_ML3_.jpg",
"rating": {
"rate": 4.6,
"count": 400
}
},
{
"id": 6,
"title": "Solid Gold Petite Micropave ",
"price": 168,
"description": "Satisfaction Guaranteed. Return or exchange any order within 30 days.Designed and sold by Hafeez Center in the United States. Satisfaction Guaranteed. Return or exchange any order within 30 days.",
"category": "jewelery",
"image": "https://fakestoreapi.com/img/61sbMiUnoGL._AC_UL640_QL65_ML3_.jpg",
"rating": {
"rate": 3.9,
"count": 70
}
},
{
"id": 7,
"title": "White Gold Plated Princess",
"price": 9.99,
"description": "Classic Created Wedding Engagement Solitaire Diamond Promise Ring for Her. Gifts to spoil your love more for Engagement, Wedding, Anniversary, Valentine's Day...",
"category": "jewelery",
"image": "https://fakestoreapi.com/img/71YAIFU48IL._AC_UL640_QL65_ML3_.jpg",
"rating": {
"rate": 3,
"count": 400
}
},
{
"id": 8,
"title": "Pierced Owl Rose Gold Plated Stainless Steel Double",
"price": 10.99,
"description": "Rose Gold Plated Double Flared Tunnel Plug Earrings. Made of 316L Stainless Steel",
"category": "jewelery",
"image": "https://fakestoreapi.com/img/51UDEzMJVpL._AC_UL640_QL65_ML3_.jpg",
"rating": {
"rate": 1.9,
"count": 100
}
},
{
"id": 9,
"title": "WD 2TB Elements Portable External Hard Drive - USB 3.0 ",
"price": 64,
"description": "USB 3.0 and USB 2.0 Compatibility Fast data transfers Improve PC Performance High Capacity; Compatibility Formatted NTFS for Windows 10, Windows 8.1, Windows 7; Reformatting may be required for other operating systems; Compatibility may vary depending on user’s hardware configuration and operating system",
"category": "electronics",
"image": "https://fakestoreapi.com/img/61IBBVJvSDL._AC_SY879_.jpg",
"rating": {
"rate": 3.3,
"count": 203
}
},
{
"id": 10,
"title": "SanDisk SSD PLUS 1TB Internal SSD - SATA III 6 Gb/s",
"price": 109,
"description": "Easy upgrade for faster boot up, shutdown, application load and response (As compared to 5400 RPM SATA 2.5” hard drive; Based on published specifications and internal benchmarking tests using PCMark vantage scores) Boosts burst write performance, making it ideal for typical PC workloads The perfect balance of performance and reliability Read/write speeds of up to 535MB/s/450MB/s (Based on internal testing; Performance may vary depending upon drive capacity, host device, OS and application.)",
"category": "electronics",
"image": "https://fakestoreapi.com/img/61U7T1koQqL._AC_SX679_.jpg",
"rating": {
"rate": 2.9,
"count": 470
}
},
{
"id": 11,
"title": "Silicon Power 256GB SSD 3D NAND A55 SLC Cache Performance Boost SATA III 2.5",
"price": 109,
"description": "3D NAND flash are applied to deliver high transfer speeds Remarkable transfer speeds that enable faster bootup and improved overall system performance. The advanced SLC Cache Technology allows performance boost and longer lifespan 7mm slim design suitable for Ultrabooks and Ultra-slim notebooks. Supports TRIM command, Garbage Collection technology, RAID, and ECC (Error Checking & Correction) to provide the optimized performance and enhanced reliability.",
"category": "electronics",
"image": "https://fakestoreapi.com/img/71kWymZ+c+L._AC_SX679_.jpg",
"rating": {
"rate": 4.8,
"count": 319
}
},
{
"id": 12,
"title": "WD 4TB Gaming Drive Works with Playstation 4 Portable External Hard Drive",
"price": 114,
"description": "Expand your PS4 gaming experience, Play anywhere Fast and easy, setup Sleek design with high capacity, 3-year manufacturer's limited warranty",
"category": "electronics",
"image": "https://fakestoreapi.com/img/61mtL65D4cL._AC_SX679_.jpg",
"rating": {
"rate": 4.8,
"count": 400
}
},
{
"id": 13,
"title": "Acer SB220Q bi 21.5 inches Full HD (1920 x 1080) IPS Ultra-Thin",
"price": 599,
"description": "21. 5 inches Full HD (1920 x 1080) widescreen IPS display And Radeon free Sync technology. No compatibility for VESA Mount Refresh Rate: 75Hz - Using HDMI port Zero-frame design | ultra-thin | 4ms response time | IPS panel Aspect ratio - 16: 9. Color Supported - 16. 7 million colors. Brightness - 250 nit Tilt angle -5 degree to 15 degree. Horizontal viewing angle-178 degree. Vertical viewing angle-178 degree 75 hertz",
"category": "electronics",
"image": "https://fakestoreapi.com/img/81QpkIctqPL._AC_SX679_.jpg",
"rating": {
"rate": 2.9,
"count": 250
}
},
{
"id": 14,
"title": "Samsung 49-Inch CHG90 144Hz Curved Gaming Monitor (LC49HG90DMNXZA) – Super Ultrawide Screen QLED ",
"price": 999.99,
"description": "49 INCH SUPER ULTRAWIDE 32:9 CURVED GAMING MONITOR with dual 27 inch screen side by side QUANTUM DOT (QLED) TECHNOLOGY, HDR support and factory calibration provides stunningly realistic and accurate color and contrast 144HZ HIGH REFRESH RATE and 1ms ultra fast response time work to eliminate motion blur, ghosting, and reduce input lag",
"category": "electronics",
"image": "https://fakestoreapi.com/img/81Zt42ioCgL._AC_SX679_.jpg",
"rating": {
"rate": 2.2,
"count": 140
}
},
{
"id": 15,
"title": "BIYLACLESEN Women's 3-in-1 Snowboard Jacket Winter Coats",
"price": 56.99,
"description": "Note:The Jackets is US standard size, Please choose size as your usual wear Material: 100% Polyester; Detachable Liner Fabric: Warm Fleece. Detachable Functional Liner: Skin Friendly, Lightweigt and Warm.Stand Collar Liner jacket, keep you warm in cold weather. Zippered Pockets: 2 Zippered Hand Pockets, 2 Zippered Pockets on Chest (enough to keep cards or keys)and 1 Hidden Pocket Inside.Zippered Hand Pockets and Hidden Pocket keep your things secure. Humanized Design: Adjustable and Detachable Hood and Adjustable cuff to prevent the wind and water,for a comfortable fit. 3 in 1 Detachable Design provide more convenience, you can separate the coat and inner as needed, or wear it together. It is suitable for different season and help you adapt to different climates",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/51Y5NI-I5jL._AC_UX679_.jpg",
"rating": {
"rate": 2.6,
"count": 235
}
},
{
"id": 16,
"title": "Lock and Love Women's Removable Hooded Faux Leather Moto Biker Jacket",
"price": 29.95,
"description": "100% POLYURETHANE(shell) 100% POLYESTER(lining) 75% POLYESTER 25% COTTON (SWEATER), Faux leather material for style and comfort / 2 pockets of front, 2-For-One Hooded denim style faux leather jacket, Button detail on waist / Detail stitching at sides, HAND WASH ONLY / DO NOT BLEACH / LINE DRY / DO NOT IRON",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/81XH0e8fefL._AC_UY879_.jpg",
"rating": {
"rate": 2.9,
"count": 340
}
},
{
"id": 17,
"title": "Rain Jacket Women Windbreaker Striped Climbing Raincoats",
"price": 39.99,
"description": "Lightweight perfet for trip or casual wear---Long sleeve with hooded, adjustable drawstring waist design. Button and zipper front closure raincoat, fully stripes Lined and The Raincoat has 2 side pockets are a good size to hold all kinds of things, it covers the hips, and the hood is generous but doesn't overdo it.Attached Cotton Lined Hood with Adjustable Drawstrings give it a real styled look.",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/71HblAHs5xL._AC_UY879_-2.jpg",
"rating": {
"rate": 3.8,
"count": 679
}
},
{
"id": 18,
"title": "MBJ Women's Solid Short Sleeve Boat Neck V ",
"price": 9.85,
"description": "95% RAYON 5% SPANDEX, Made in USA or Imported, Do Not Bleach, Lightweight fabric with great stretch for comfort, Ribbed on sleeves and neckline / Double stitching on bottom hem",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/71z3kpMAYsL._AC_UY879_.jpg",
"rating": {
"rate": 4.7,
"count": 130
}
},
{
"id": 19,
"title": "Opna Women's Short Sleeve Moisture",
"price": 7.95,
"description": "100% Polyester, Machine wash, 100% cationic polyester interlock, Machine Wash & Pre Shrunk for a Great Fit, Lightweight, roomy and highly breathable with moisture wicking fabric which helps to keep moisture away, Soft Lightweight Fabric with comfortable V-neck collar and a slimmer fit, delivers a sleek, more feminine silhouette and Added Comfort",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/51eg55uWmdL._AC_UX679_.jpg",
"rating": {
"rate": 4.5,
"count": 146
}
},
{
"id": 20,
"title": "DANVOUY Womens T Shirt Casual Cotton Short",
"price": 12.99,
"description": "95%Cotton,5%Spandex, Features: Casual, Short Sleeve, Letter Print,V-Neck,Fashion Tees, The fabric is soft and has some stretch., Occasion: Casual/Office/Beach/School/Home/Street. Season: Spring,Summer,Autumn,Winter.",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/61pHAEJ4NML._AC_UX679_.jpg",
"rating": {
"rate": 3.6,
"count": 145
}
}
]
如果仔细观察,JSON 响应看起来像 Python 字典列表。JSON 是 REST API 中非常流行的数据交换格式。
您还可以打印与响应相关的其他属性,例如状态代码。
print(response.status_code)
# OUTPUT
>>> 200
我们知道,状态代码 200 表示成功响应。
由于 /products
端点返回大量数据,我们将这些数据限制为仅 3 种产品。
为此,我们有一个端点 /products?limit=x
,其中 x 是一个正整数。这 limit
称为查询参数。让我们看看如何在请求中添加此查询参数。
import requests
BASE_URL = 'https://fakestoreapi.com'
query_params = {
"limit": 3
}
response = requests.get(f"{BASE_URL}/products", params=query_params)
print(response.json())
该 requests.get()
方法接受一个名为的参数, params
我们可以在其中以 Python 字典的形式指定查询参数。因此,我们创建了一个名为的字典 query_params
,并将其 limit
作为键和 3
值传递。然后我们将其传递 query_params
到中 requests.get()
。输出现在如下所示:
[
{
"id": 1,
"title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
"rating": { "rate": 3.9, "count": 120 }
},
{
"id": 2,
"title": "Mens Casual Premium Slim Fit T-Shirts ",
"price": 22.3,
"description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg",
"rating": { "rate": 4.1, "count": 259 }
},
{
"id": 3,
"title": "Mens Cotton Jacket",
"price": 55.99,
"description": "great outerwear jackets for Spring/Autumn/Winter, suitable for many occasions, such as working, hiking, camping, mountain/rock climbing, cycling, traveling or other outdoors. Good gift choice for you or your family member. A warm hearted love to Father, husband or son in this thanksgiving or Christmas Day.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71li-ujtlUL._AC_UX679_.jpg",
"rating": { "rate": 4.7, "count": 500 }
}
]
现在我们将响应数据限制为仅 3 种产品。让我们尝试从 id
18 种产品中仅获取一种产品。
import requests
BASE_URL = 'https://fakestoreapi.com'
response = requests.get(f"{BASE_URL}/products/18")
print(response)
由于我们有一个端点 /products/<product_id>
,我们可以 id
在 API URL 中传递 18 并对其发出 GET 请求。响应如下所示:
{
"id": 18,
"title": "MBJ Women's Solid Short Sleeve Boat Neck V ",
"price": 9.85,
"description": "95% RAYON 5% SPANDEX, Made in USA or Imported, Do Not Bleach, Lightweight fabric with great stretch for comfort, Ribbed on sleeves and neckline / Double stitching on bottom hem",
"category": "women's clothing",
"image": "https://fakestoreapi.com/img/71z3kpMAYsL._AC_UY879_.jpg",
"rating": {
"rate": 4.7,
"count": 130
}
}
如何发出 POST 请求
我们使用 POST 请求向 REST API 添加新数据。数据以 JSON 格式发送到服务器,该格式看起来像 Python 字典。根据 Fake Store API 文档,产品具有以下属性: title
, price
, description
, image
和 category
。因此,新产品如下所示:
new_product = {
"title": 'test product',
"price": 13.5,
"description": 'lorem ipsum set',
"image": 'https://i.pravatar.cc',
"category": 'electronic'
}
如下方法 requests.post()
发送 POST 请求
import requests
BASE_URL = 'https://fakestoreapi.com'
new_product = {
"title": 'test product',
"price": 13.5,
"description": 'lorem ipsum set',
"image": 'https://i.pravatar.cc',
"category": 'electronic'
}
response = requests.post(f"{BASE_URL}/products", json=new_product)
print(response.json())
在该 requests.post()
方法中,我们可以使用参数传递 JSON 数据 json
。使用 json
参数会自动将 Content-Type
请求标头中的 Application/JSON
为
一旦我们在端点上发出 POST 请求 /products
,我们就会得到一个带有响应的产品对象 id
。响应如下所示:
{
"_id": "61b45067e087f30012c45a45",
"id": 21,
"title": "test product",
"price": 13.5,
"description": "lorem ipsum set",
"image": "https://i.pravatar.cc",
"category": "electronic"
}
如果我们不使用该 json
参数,我们必须像这样发出 POST 请求:
import requests
import json
BASE_URL = 'https://fakestoreapi.com'
new_product = {
"title": 'test product',
"price": 13.5,
"description": 'lorem ipsum set',
"image": 'https://i.pravatar.cc',
"category": 'electronic'
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(f"{BASE_URL}/products", data=json.dumps(new_product), headers=headers)
print(response.json())
在这种情况下,我们使用参数 data
而不是 json
,我们需要 Content-Type
在标题中明确设置。而在参数的情况下 application/json
, json
我们不需要序列化数据——但 json.dumps()
在这种情况下,我们需要使用序列化数据。
如何发出 PUT 请求
我们经常需要更新 API 中的现有数据。使用 PUT 请求,我们可以更新完整数据。这意味着当我们发出 PUT 请求时,它会用新数据替换旧数据。
在 POST 请求中,我们创建了一个 id
名为 21 的新产品。让我们通过在端点上发出 PUT 请求来用新产品更新旧产品 products/<product_id>
。
import requests
BASE_URL = 'https://fakestoreapi.com'
updated_product = {
"title": 'updated_product',
"category": 'clothing'
}
response = requests.put(f"{BASE_URL}/products/21", json=updated_product)
print(response.json())
使用该 updated_product
发出 PUT 请求时 requests.put()
,它会使用以下 JSON 数据进行响应:
{
"id": "21",
"title": "updated_product",
"category": "clothing"
}
请注意,旧产品已被更新产品完全取代。
如何提出 PATCH 请求
有时,我们不需要完全替换旧数据。而是希望仅修改某些字段。在这种情况下,我们使用 PATCH 请求。
通过在端点上发出 PATCH 请求 将产品类别从 服装 回 电子产品 products/<product_id>
。
import requests
BASE_URL = 'https://fakestoreapi.com'
updated_product = {
"category": 'electronic'
}
response = requests.patch(f"{BASE_URL}/products/21", json=updated_product)
print(response.json())
在这种情况下,我们使用 requests.patch()
返回如下响应的方法:
{
"id": "21",
"title": "updated_product",
"category": "electronic"
}
请注意,这次整个数据没有改变——只有类别字段已被更新。
如何发出删除请求
顾名思义,如果您希望从 API 中删除资源,则可以使用 DELETE 请求。让我们删除这个编号为 id
21 的产品。
import requests
BASE_URL = 'https://fakestoreapi.com'
response = requests.delete(f"{BASE_URL}/products/21")
print(response.json())
该 requests.delete()
方法帮助我们在端点上发出 DELETE 请求 /products/<product_id>
。
包起来
Python 中 请求 的出色工具与 Web 服务进行交互
希望您喜欢它——感谢您的阅读!
发表评论 取消回复