根据淘宝客API搜索优惠券

淘宝客大家应该不陌生,专门发各种淘宝优惠券的平台,分发的人可以赚取佣金,今天说来说下使用python调取淘宝客api搜索优惠券的正确姿势

安装topsdk

topsdk 是淘宝客python接口sdk,集成比较全面,也可以在阿里联盟自行下载官方的sdk(有的解决没有提供,需要自己来写)

(venv) $ pip install topsdk

获取优惠券的接口

top.api.TbkDgMaterialOptionalRequest()

实现代码

import top
import re
import requests
import json

appkey = '123'
secret = 'asdfasfweasf'
adzone_id = '234345345435'


# 获取淘口令
def get_token(url, text):
    req = top.api.TbkTpwdCreateRequest()
    req.set_app_info(top.appinfo(appkey, secret))

    req.text = text
    req.url = url
    try:
        resp = req.getResponse()['tbk_tpwd_create_response']['data']['model']
        return resp
    except Exception as e:
        print(e)
        return None

# 通过淘宝客API搜索优惠券
def get_tk_coupon(kw,size=5):

    req = top.api.TbkDgMaterialOptionalRequest()
    req.set_app_info(top.appinfo(appkey, secret))

    req.adzone_id = int(adzone_id)
    req.platform = 2
    req.page_size = size
    req.q = kw
    req.page_no = 1
    try:
        # print('===',req.getResponse())
        resp = req.getResponse()['tbk_dg_material_optional_response']['result_list']['map_data']
        return resp
    except Exception as e:
        print(e)
        return None

if __name__ == "__main__":
    response = get_tk_coupon('金稻直发梳夹板直发卷发两用神器卷发棒不伤发负离子懒人女迷小型')
    # print(resp)

    for r in response:
        # 商品标题
        ordername = r['title']
        # 商品当前价
        orderprice = r['zk_final_price']
        coupon_info = r['coupon_info']
        coupon_demonination = int(re.findall(r'(\d+)', coupon_info)[-1])
        # 商品图片
        orderimg = r['pict_url']
        # 获取淘口令
        token = get_token(url='https:' + r['url'], text=r['title'])
        # 券后价
        couponprice = round(float(orderprice) - int(coupon_demonination), 1)
        link = r['item_url']
        msgs = '''/:gift{name}\n/:rose【在售价】{orderprice}\n/:heart【券后价】{conponprice}\n/:cake 【抢购链接】{link_short}\n-----------------\n复制这条信息\n{token}打开【手机淘宝】,即可查看\n------------------\n
            '''.format(name=ordername, orderprice=orderprice, conponprice=couponprice, token=token,
                link_short=link)
        print(msgs)