以下以使用一个虚构的获取天气信息的 API 接口为例,展示在 Python 语言中如何使用封装好的 API 接口,这里假设 API 接口基于 RESTful 风格,使用 requests 库来发起请求。
1. API 接口相关设定(模拟)
- 接口地址: https://taobao/item_get/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=652874751412&is_promotion=1 ,用于获取淘宝商品信息。
- 请求方法:GET。
- 请求参数:
- 请求参数:num_iid=652874751412&is_promotion=1
- 参数说明:num_iid:淘宝商品ID is_promotion:是否获取取促销价
-
响应数据格式:
| 名称 | 类型 | 必须 | 示例值 | 描述 |
|---|---|---|---|---|
| item | item[] | 1 | 宝贝详情数据 | |
| num_iid | Bigint | 1 | 520813250866 | 宝贝ID |
| title | String | 1 | 三刃木折叠刀过安检创意迷你钥匙扣钥匙刀军刀随身多功能小刀包邮 | 宝贝标题 |
| desc_short | String | 0 | 商品简介 | |
| promotion_price | Int | 0 | 优惠价 | |
| price | Float | 1 | 25.8 | 价格(当商品价格为0时,当前值为-1),价格来自于未登入的划线价格,会有个别商品价格无法与页面匹配 |
| total_price | Float | 0 | 0 | |
| suggestive_price | Float | 0 | 0 | |
| orginal_price | String | 0 | 25.80 | 原价 |
| nick | String | 0 | 欢乐购客栈 | 掌柜昵称 |
| num | Int | 0 | 3836 | 库存(没有精确,是模糊值) |
| min_num | Int | 0 | 0 | 最小购买数 |
| detail_url | String | 0 | item.taobao.com/item.htm?id… | 宝贝链接 |
| pic_url | String | 1 | //gd2.alicdn.com/imgextra/i4/2596264565/TB2p30elFXXXXXQXpXXXXXXXXXX_!!2596264565.jpg | 宝贝图片 |
| brand | String | 0 | 三刃木 | 品牌名称 |
| brandId | Int | 0 | 8879363 | 品牌ID |
| rootCatId | Int | 0 | 50013886 | 顶级分类ID |
| cid | Int | 1 | 50014822 | |
| crumbs | Mix | 0 | [] | 导航菜单 |
| created_time | String | 0 | ||
| modified_time | String | 0 | ||
| delist_time | String | 0 | ||
| desc | String | 0 | 商品详情 | |
| desc_img | Mix | 0 | [] | 商品详情图片 |
| item_imgs | Mix | 0 | item_imgs[] | 商品图片 |
| url | string | 0 | //gd3.alicdn.com/imgextra/i4/2596264565/TB2p30elFXXXXXQXpXXXXXXXXXX_!!2596264565.jpg | |
| item_weight | String | 0 | ||
| item_size | String | 0 | ||
| location | String | 0 | 发货地 | |
| express_fee | Float | 0 | 0.00 | 快递费用 |
| ems_fee | Float | 0 | EMS费用 | |
| post_fee | Float | 0 | 物流费用 | |
| shipping_to | String | 0 | 发货至 | |
| has_discount | Boolean | 0 | false | 是否有优惠 |
| video | video[] | 0 | 商品视频 | |
| pic | string | 0 | img.alicdn.com/imgextra/i4… | 视频预览图 |
| url | string | 0 | //cloud.video.taobao.com/play/u/1795410438/p/1/e/1/t/8/218504165570.swf | 视频路径 |
| type | string | 0 | item | 视频类型 |
| is_virtual | String | 0 | ||
| is_promotion | Boolean | 0 | false | 是否促销 |
| props_name | String | 0 | 1627207:1347647754:颜色分类:长方形带开瓶器+送工具刀卡+链子;1627207:1347647753:颜色分类:椭圆形带开瓶器+送工具刀卡+链子; | 商品属性名。格式为pid1:vid1:name1:value1;pid1:vid2:name2:value2。 |
| prop_imgs | prop_imgs[] | 0 | 商品属性图片列表 | |
| prop_img | prop_img[] | 0 | ||
| properties | string | 0 | 1627207:1347647754 | |
| url | string | 0 | //gd2.alicdn.com/imgextra/i3/2596264565/TB2.XeblVXXXXXkXpXXXXXXXXXX_!!2596264565.jpg | |
| property_alias | String | 0 | 20509:9974422:36;1627207:28326:红色;20509:9975710:38;1627207:28326:红色;20509:9981357:40;1627207:28326:红色 | 销售属性值别名。格式为pid1:vid1:alias1;pid1:vid2:alia2。 |
| props | Mix | 0 | [{ "name": "产地","value": "中国" }] | 商品属性 |
| total_sold | Int | 0 | ||
| skus | skus[] | 0 | 商品规格信息列表 | |
| sku | sku[] | 0 | ||
| price | string | 0 | 39 | |
| total_price | string | 0 | ul | |
| orginal_price | string | 0 | 39.00 | |
| properties | string | 0 | 1627207:1347647754", | |
| properties_name | string | 0 | 1627207:1347647754:颜色分类:长方形带开瓶器+送工具刀卡+链子 | |
| quantity | string | 0 | 305 | |
| sku_id | string | 0 | 316659862598 | |
| seller_id | Int | 0 | 2844096782 | 卖家ID |
| sales | Int | 0 | 138 | 销量 |
| shop_id | Int | 0 | 151372205 | 店铺ID |
| props_list | Mix | 0 | {20509:9974422: 尺码:36} | 商品属性 |
| seller_info | seller_info[] | 1 | 卖家信息 | |
| nick | string | 1 | 欢乐购客栈 | |
| shop_type | string | 0 | ||
| user_num_id | Bigint | 0 | 2596264565 | |
| sid | Bigint | 0 | 127203758 | |
| title | string | 0 | 欢乐购客栈 | |
| zhuy | string | 0 | //shop127203758.taobao.com | |
| company_name | string | 0 | ||
| tmall | Boolean | 0 | false | 是否天猫 |
| error | String | 0 | 错误信息 | |
| warning | String | 0 | 警告信息 | |
| url_log | Mix | 0 | [] | |
| favcount | Int | 0 | 0 | |
| fanscount | Int | 0 | 0 | |
| method | String | 0 | item_tmall:pget_item | |
| promo_type | String | 0 | ||
| props_img | Mix | 0 | 1627207:28326": "//img.alicdn.com/imgextra/i2/2844096782/O1CN01VrjpXt1zyCc9DvERE_!!2844096782.jpg | 属性图片 |
| shop_item | Mix | 0 | [] | |
| relate_items | Mix | 0 | [] |
2. Python 代码示例
# coding:utf-8
"""
Compatible for python2.x and python3.x
requirement: pip install requests
"""
from __future__ import print_function
import requests
# 请求示例 url 默认请求参数已经做URL编码
url = "https://taobao/item_get/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=652874751412&is_promotion=1"
headers = {
"Accept-Encoding": "gzip",
"Connection": "close"
}
if __name__ == "__main__":
r = requests.get(url, headers=headers)
json_obj = r.json()
print(json_obj)
在上述示例中:
-
首先定义了 API 接口的地址 url 和请求参数 num id,将商品链接和对应的 API 身份验证 appid 填入(实际应用中要替换为真实有效的信息)。
-
接着使用 requests 库的 get 方法向接口发起请求,根据返回的状态码判断请求是否成功。
-
如果状态码为 0000,就通过 response.json() 解析返回的 JSON 格式数据,提取商品详情信息展示出来。
-
若出现请求异常(比如网络问题、参数错误等导致请求失败),则捕获异常并打印相应的错误提示信息。
请注意,这只是一个示例,实际使用不同的 API 接口时,要严格按照对应接口的文档要求来设置请求参数、处理响应数据以及进行可能的身份认证等操作。