本文介绍淘宝联盟服务Python SDK的使用方法及示例。
接口列表
接口名称 | 接口说明 |
SearchByPic | 用于向淘宝联盟版在线调用,根据图片查询。 |
SearchByUrl | 用于向淘宝联盟版在线调用,根据图片URL查询。 |
GetProductInfoByIds | 用于向淘宝联盟版在线调用,根据ID查询商品。 |
准备工作
在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见
使用如下方式安装依赖包。
pip install alibabacloud_imagesearch20210501
SearchByPic接口
代码示例
from alibabacloud_tea_openapi.models import Config
from alibabacloud_imagesearch20210501.client import Client
from alibabacloud_imagesearch20210501.models import SearchByUrlRequest, SearchByPicAdvanceRequest
from alibabacloud_tea_util.models import RuntimeOptions
import os
config = Config()
#阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
#强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
#本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
config.access_key_id = os.environ['CC_AK_ENV']
config.access_key_secret = os.environ['CC_SK_ENV']
config.endpoint = 'imagesearch.cn-shanghai.aliyuncs.com'
config.region_id = 'cn-shanghai'
client = Client(config)
runtime_option = RuntimeOptions()
PIC_URL = "https://XX.oss-cn-shanghai.aliyuncs.com/m.jpg";
PIC_NAME = "C:\\XX/9.jpg";
# 根据图片Url查询
def SearchPic():
request = SearchByPicAdvanceRequest()
f = open(PIC_NAME, 'rb')
request.pic_content_object = f
# 必填,PID
request.pid = "mm_XX_XX_XX"
# 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
# request.fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
# 选填,图片类目
#request.categoryId = 88888888;
# 选填,是否需要进行主体识别。默认true
#request.crop = false;
# 选填,图片的主体区域,格式为x1,x2,y1,y2
#request.region = "518,1524,398,1632";
# 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
request.start = 0;
# 选填,返回结果的数目。取值范围:1-20。默认值:10。
request.num = 1;
# 选填,渠道ID。用于淘宝联盟中的渠道区分。
#request.relationId = 1145;
response = client.search_by_pic_advance(request,runtime_option)
print('SearchPic ', response.to_map())
if __name__ == '__main__':
SearchPic()
结果示例
{
'headers': {
'Date': 'Wed, 04 Aug 2021 07:27:11 GMT',
'Content-Type': 'application/json;charset=utf-8',
'Transfer-Encoding': 'chunked',
'Connection': 'keep-alive',
'Vary': 'Accept-Encoding',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx',
'Access-Control-Max-Age': '172800',
'x-acs-request-id': 'xx-2260-xx-A6C9-xx',
'Content-Encoding': 'gzip'
},
'body': {
'Success': True,
'Code': 0,
'Data': {
'Auctions': [
{
'Result': {
'Title': '免粘胶',
'PicUrl': '//img.alicdn.com/456.jpg',
'ReservePrice': '9.9',
'ZkFinalPrice': '9.9',
'Url': '//s.click.taobao.com/txxxx'
},
'RankScore': 0.8486
}
]
},
'RequestId': 'xx-2260-xx-A6C9-xx',
'PicInfo': {
'MainRegion': {
'Region': '88,453,68,411',
'MultiCategoryId': [
{
'CategoryId': '88888888',
'Score': 0.72653
},
{
'CategoryId': '20',
'Score': 0.05694
},
{
'CategoryId': '9',
'Score': 0.05302
},
{
'CategoryId': '8',
'Score': 0.0404
}
]
},
'MultiRegion': [
{
'Region': '88,453,68,411'
},
{
'Region': '88,453,68,411'
},
{
'Region': '88,453,68,411'
}
]
}
}
}
SearchByUrl接口
代码示例
from alibabacloud_tea_openapi.models import Config
from alibabacloud_imagesearch20210501.client import Client
from alibabacloud_imagesearch20210501.models import SearchByUrlRequest, SearchByPicAdvanceRequest
from alibabacloud_tea_util.models import RuntimeOptions
import os
config = Config()
#阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
#强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
#本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
config.access_key_id = os.environ['CC_AK_ENV']
config.access_key_secret = os.environ['CC_SK_ENV']
config.endpoint = 'imagesearch.cn-shanghai.aliyuncs.com'
config.region_id = 'cn-shanghai'
client = Client(config)
runtime_option = RuntimeOptions()
PIC_URL = "https://XX.oss-cn-shanghai.aliyuncs.com/m.jpg";
PIC_NAME = "C:\\XX/9.jpg";
# 根据图片Url查询
def SearchUrl():
request = SearchByUrlRequest()
request.pic_url = PIC_URL
# 必填,PID
request.pid = "mm_XX_XXX_XXX"
# 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
# request.fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
# 选填,图片类目
#request.categoryId = 88888888;
# 选填,是否需要进行主体识别。默认true
#request.crop = false;
# 选填,图片的主体区域,格式为x1,x2,y1,y2
#request.region = "518,1524,398,1632";
# 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
request.start = 0;
# 选填,返回结果的数目。取值范围:1-20。默认值:10。
request.num = 1;
# 选填,渠道ID。用于淘宝联盟中的渠道区分。
#request.relationId = 1145;
response = client.search_by_url(request)
print('SearchUrl ', response.to_map())
if __name__ == '__main__':
SearchUrl()
结果示例
{
'headers': {
'Date': 'Wed, 04 Aug 2021 07:27:12 GMT',
'Content-Type': 'application/json;charset=utf-8',
'Transfer-Encoding': 'chunked',
'Connection': 'keep-alive',
'Vary': 'Accept-Encoding',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx',
'Access-Control-Max-Age': '172800',
'x-acs-request-id': 'xx-xx-xxx-xxx-xxx',
'Content-Encoding': 'gzip'
},
'body': {
'Success': True,
'Code': 0,
'Data': {
'Auctions': [
{
'Result': {
'Title': 'xxxxxx',
'PicUrl': '//img.alicdn.com/pic.jpg',
'ReservePrice': '7.8',
'ZkFinalPrice': '7.8',
'Url': '//s.click.taobao.com/txxxx'
},
'RankScore': 0.71
}
]
},
'RequestId': 'xx-xxx-xx-xxx-xx',
'PicInfo': {
'MainRegion': {
'Region': '263,1238,259,715',
'MultiCategoryId': [
{
'CategoryId': '88888888',
'Score': 0.89114
},
{
'CategoryId': '22',
'Score': 0.02755
},
{
'CategoryId': '8',
'Score': 0.01099
},
{
'CategoryId': '6',
'Score': 0.01038
}
]
},
'MultiRegion': [
{
'Region': '263,1238,259,715'
},
{
'Region': '263,1238,259,715'
}
]
}
}
}
GetProductInfoByIds接口
代码示例
from alibabacloud_tea_openapi.models import Config
from alibabacloud_imagesearch20210501.client import Client
from alibabacloud_imagesearch20210501.models import GetProductInfoByIdsRequest
from alibabacloud_tea_util.models import RuntimeOptions
import os
config = Config()
#阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
#强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
#本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
config.access_key_id = os.environ['CC_AK_ENV']
config.access_key_secret = os.environ['CC_SK_ENV']
config.endpoint = 'imagesearch.cn-shanghai.aliyuncs.com'
config.region_id = 'cn-shanghai'
client = Client(config)
runtime_option = RuntimeOptions()
# 根据图片Url查询
def getProduct():
request = GetProductInfoByIdsRequest()
# 必填,PID
request.pid = "mm_1123123_22e121_134431242"
# 必填商品ID串,用','分割,最大40个。
request.item_ids = "587928509576"
# 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
request.fields = "CommissionRate"
response = client.get_product_info_by_ids(request)
print('getProduct ', response.body.to_map())
if __name__ == '__main__':
getProduct()
结果示例
{
'Code': 0,
'Data': {
'Auctions': [
{
'Result': {
'CommissionRate': '300',
'MaxCommission': {
},
'PicUrl': 'zxccas.jpg',
'ReservePrice': '35',
'Title': 'asdasd',
'Url': 'xxxxxxxxxx',
'ZkFinalPrice': '28'
}
}
]
},
'RequestId': 'qwsdas-czxczx-asdac-ascas-asdasd',
'Success': True
}
文档内容是否对您有帮助?