全部产品

Python SDK

本文介绍淘宝联盟服务Python SDK的使用方法及示例。

接口列表

接口名称

接口说明

SearchByPic

用于向淘宝联盟版在线调用,根据图片查询。

SearchByUrl

用户向淘宝联盟版在线调用,根据图片Url查询

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见

    创建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_oss_util.models import RuntimeOptions

config = Config()
config.access_key_id = 'xxxxx'
config.access_key_secret = 'xxxxxx'
config.endpoint = 'imagesearch.cn-shanghai.aliyuncs.com'
config.region_id = 'cn-shanghai'
client = Client(config)
runtime_option = RuntimeOptions()
PIC_NAME = "C:\\xxxx/9.jpg";


# 根据图片查询
def SearchPic():
    request = SearchByPicAdvanceRequest()
    f = open(PIC_NAME, 'rb')
    request.pic_content_object = f
    # 需要返回的字段list。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
    request.fields = "Provcity,UserType,CouponTotalCount,ItemId";
    # 图片类目
    #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)
    f.close()
    print('SearchPic   ', response.to_map())


if __name__ == '__main__':
    SearchPic()

  • 结果示例

SearchPic
{
  'headers': {
    'Date': 'Tue, 08 Jun 2021 07:40:37 GMT',
    'Content-Type': 'application/json;charset=utf-8',
    'Content-Length': '932',
    'Connection': 'keep-alive',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
    'Access-Control-Allow-Headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signature',
    'Access-Control-Max-Age': '172800',
    'x-acs-request-id': 'EBD06D10-BDD7-45C0-8896-44444444'
  },
  'body': {
    'Success': True,
    'Code': 0,
    'Data': {
      'Auctions': [
        {
          'Result': {
            'ItemId': '558112896177',
            'ItemName': 'PPR三通接头',
            'Pic': '//img.alicdn.com/i1/xxxxxx.jpg',
            'Price': '0.12',
            'PromotionPrice': '0.12',
            'UserType': 0,
            'Provcity': '山东 临沂',
            'ClickUrl': '//s.click.taobao.com/t?yyyyyy'
          },
          'RankScore': 0.8301
        }
      ]
    },
    'RequestId': 'EBD06D10-BDD7-45C0-8896-444444',
    'PicInfo': {
      'MainRegion': {
        'Region': '55,456,65,415',
        'MultiCategoryId': [
          {
            'CategoryId': '88888888',
            'Score': 0.7559
          },
          {
            'CategoryId': '20',
            'Score': 0.05169
          },
          {
            'CategoryId': '8',
            'Score': 0.04217
          },
          {
            'CategoryId': '9',
            'Score': 0.03845
          }
        ]
      },
      'MultiRegion': [
        {
          'Region': '55,456,65,415'
        }
      ]
    }
  }
}

SearchByUrl接口

  • 代码示例

from alibabacloud_tea_openapi.models import Config
from alibabacloud_imagesearch20210501.client import Client
from alibabacloud_imagesearch20210501.models import SearchByUrlRequest, SearchByPicAdvanceRequest
from alibabacloud_oss_util.models import RuntimeOptions

config = Config()
config.access_key_id = 'xxxxx'
config.access_key_secret = 'xxxxx'
config.endpoint = 'imagesearch.cn-shanghai.aliyuncs.com'
config.region_id = 'cn-shanghai'
client = Client(config)
runtime_option = RuntimeOptions()
PIC_URL = "https://ossname.oss-cn-shanghai.aliyuncs.com/m.jpg";


# 根据图片Url查询
def SearchUrl():
    request = SearchByUrlRequest()
    request.pic_url = PIC_URL
    # 需要返回的字段list。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
    request.fields = "Provcity,UserType,CouponTotalCount,ItemId";
    # 图片类目
    #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()

  • 结果示例

SearchUrl
{
  'headers': {
    'Date': 'Tue, 08 Jun 2021 07:40:37 GMT',
    'Content-Type': 'application/json;charset=utf-8',
    'Content-Length': '932',
    'Connection': 'keep-alive',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
    'Access-Control-Allow-Headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signature',
    'Access-Control-Max-Age': '172800',
    'x-acs-request-id': 'EBD06D10-BDD7-45C0-8896-44444444'
  },
  'body': {
    'Success': True,
    'Code': 0,
    'Data': {
      'Auctions': [
        {
          'Result': {
            'ItemId': '558112896177',
            'ItemName': 'PPR三通接头',
            'Pic': '//img.alicdn.com/i1/xxxxxx.jpg',
            'Price': '0.12',
            'PromotionPrice': '0.12',
            'UserType': 0,
            'Provcity': '山东 临沂',
            'ClickUrl': '//s.click.taobao.com/t?yyyyyy'
          },
          'RankScore': 0.8301
        }
      ]
    },
    'RequestId': 'EBD06D10-BDD7-45C0-8896-444444',
    'PicInfo': {
      'MainRegion': {
        'Region': '55,456,65,415',
        'MultiCategoryId': [
          {
            'CategoryId': '88888888',
            'Score': 0.7559
          },
          {
            'CategoryId': '20',
            'Score': 0.05169
          },
          {
            'CategoryId': '8',
            'Score': 0.04217
          },
          {
            'CategoryId': '9',
            'Score': 0.03845
          }
        ]
      },
      'MultiRegion': [
        {
          'Region': '55,456,65,415'
        }
      ]
    }
  }
}