要做一个淘宝客的微信公众号,想实现用户手淘分享商品公众号查内部优惠券的功能,但是淘宝联盟目前没有开放通过淘宝商品id查询优惠券、佣金信息的接口,好在有提供通用物料搜索api。

大体思路是解析手淘分享内容,找出商品关键字,然后通过商品关键字使用淘宝 通用物料搜索api查到佣金、优惠券信息。实现最终效果如下:

实现

1 申请淘宝联盟appkey、secret

因为需要使用到淘宝接口,所以需要申请淘宝appkey、secret。需要开通淘宝客基础api和淘口令api,请自行前往淘宝联盟申请。

2 下载淘宝联盟sdk

需要申请淘宝appkey、secret之后在淘宝开放平台下载淘宝联盟SDK。

3 封装淘宝接口

目前只需要用到通用物料搜索api和淘宝客淘口令api,所以封装了这两个接口。
代码如下:

    /// <summary>/// 淘宝物料搜索接口,获取佣金、优惠券信息/// </summary>/// <param name="key"></param>/// <returns></returns>public IList<MapDataDomain> MaterialOptional(string key){if (string.IsNullOrEmpty(appkey)||string.IsNullOrEmpty(secret)||addzoneId==0){throw new Exception("请检查是否设置接口url、淘宝appkey、secret及addzoneId");}ITopClient client = new DefaultTopClient(apiUrl,appkey,secret);TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();req.AdzoneId = addzoneId;req.Platform = 2L;req.PageSize = 100L;req.Q = key;req.PageNo = 1L;TbkDgMaterialOptionalResponse rsp = client.Execute(req);return rsp.ResultList;}/// <summary>/// 获取淘口令/// </summary>/// <param name="url"></param>/// <param name="log_url"></param>/// <returns></returns>public string GetTaobaoKePassword(string url, string log_url){ITopClient client = new DefaultTopClient(apiUrl, appkey, secret);TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();if (url.Substring(0, 4) != "http"){url = "https:" + url;}req.Text = "关注“网购有券”,超值活动,惊喜多多!";req.Url = url;req.Logo = log_url;TbkTpwdCreateResponse rsp = client.Execute(req);return rsp.Data.Model;}

4. 解析商品关键字,并查询佣金、优惠券信息。

解析商品关键字有两种方式,一种是通过抓取淘宝分享内容中“【】”里的内容,将其作为关键字查询;另外一种是通过淘宝分享内容中的超链接,通过抓包方式获取关键字。
这两种都有些缺陷,第一种可能手淘版本的区别,会存在“【】”中内容不是淘宝商品关键字的情况,另外一种存在较小概率抓包失败的情况。所以目前这两种方式都需要用到。
第一种通过抓取淘宝分享内容中“【】”做关键字实现如下:

  public string QueryCoupon(string itemInfo){string responeMessage = "";try{Match m_title = Regex.Match(itemInfo, @"【.*】");string temp = m_title.Value;if (!string.IsNullOrEmpty(temp)){temp = temp.Substring(1, temp.Length - 2);}else{return "";}if (temp.Contains("#手聚App")){int IndexofA = temp.IndexOf("宝贝不错:");int IndexofB = temp.IndexOf("(分享自");temp = temp.Substring(IndexofA + 5, IndexofB - IndexofA - 5);}string title = temp;//解决手淘分享【】内非商品关键字bugif (title.Contains(",") | title.Contains(",")){return GetTaobaoCouponByLink(itemInfo);}var resultList = taobaoCommonApi.MaterialOptional(title);if (resultList!=null&&resultList.Count > 0){//获取淘宝短链接Match m_url = Regex.Match(itemInfo, @"htt(p|ps):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?");if (m_url.Value == ""){return responeMessage;}var s = HttpUtility.HttpGet(m_url.Value, "", "utf-8");//获取宝贝item idMatch m_item = Regex.Match(s, @"((?<=m.taobao.com\/i)([0-9]+))|((?<=&id=)([0-9]+))");string item_id = m_item.Value;if (string.IsNullOrEmpty(item_id)){Match am_url = Regex.Match(s, @"(?<=var url = ')(.*)(?=')");var htmlContent = HttpUtility.HttpGet(am_url.Value, "", "gbk");Match re_m_item = Regex.Match(htmlContent, @"(?<=taobao.com/item.htm\?id=)([0-9]*)");item_id = re_m_item.Value;}if (string.IsNullOrEmpty(item_id)){var g = resultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(w => w.Volume).FirstOrDefault();if (g == null){responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");}else{var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";}return responeMessage;}else{float numid = 0;try{numid = float.Parse(item_id);}catch (Exception ex){var g = resultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault();var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";return responeMessage;}foreach (var g in resultList){if (g.NumIid == numid){if (string.IsNullOrEmpty(g.CouponInfo)){var hongbao = decimal.Parse(g.ZkFinalPrice) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【约返利】{Math.Round(hongbao, 2)}元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(g.Url, g.PictUrl + "_400x400.jpg")}\n==========================\n下单确认收货后就能收到返利佣金啦~\n 点击查看  <a href='http://mp.weixin.qq.com/s?__biz=Mzg2NTAxOTEyMA==&mid=100000146&idx=1&sn=62405c8df3db46e74940aefb9ac3737b&chksm=4e61340d7916bd1bf645afbc6d10c1f19561d7fa59847516c01e64c0791e6d544f4f56c4f498#rd'>如何领取返利</a>";return responeMessage;}else{var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";return responeMessage;}}}//没有找到,有相似宝贝推荐var w = resultList.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault();if (w == null){responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");}else{var hongbao = (decimal.Parse(w.ZkFinalPrice) - decimal.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(w.CommissionRate) / 10000 * commission_rate;responeMessage = $"/:rose 亲,这款商品的优惠返利活动结束了~\n已为你推荐以下宝贝。\n==========================\n{w.Title}\n【在售价】{w.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(w.ZkFinalPrice) - double.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(w.CouponShareUrl, w.PictUrl + "_400x400.jpg")}\n";}return responeMessage;}}else{responeMessage = GetTaobaoCouponByLink(itemInfo);}}catch (Exception ex){Senparc.Weixin.WeixinTrace.SendCustomLog("查询淘宝优惠券异常",ex.Message);}return responeMessage;}

第二种通过超链接抓包方式获取关键字方式,代码如下:

 public string GetTaobaoCouponByLink(string itemInfo){string tbk_nocoupon_msg = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");Match m_url = Regex.Match(itemInfo, @"htt(p|ps):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?");if (m_url.Value == ""){return tbk_nocoupon_msg;}var s = HttpUtility.HttpGet(m_url.Value, "", "utf-8");//获取宝贝item idMatch m_item = Regex.Match(s, @"((?<=m.taobao.com\/i)([0-9]+))|((?<=&id=)([0-9]+))");string item_id = m_item.Value;Match am_url = Regex.Match(s, @"(?<=var url = ')(.*)(?=')");var htmlContent = HttpUtility.HttpGet(am_url.Value, "", "gbk");Match keyMatch = Regex.Match(htmlContent, "(?<=title\\>).*(?=</title)");if (string.IsNullOrEmpty(item_id)){Match re_m_item = Regex.Match(htmlContent, @"(?<=taobao.com/item.htm\?id=)([0-9]*)");item_id = re_m_item.Value;}if (string.IsNullOrEmpty(keyMatch.Value)){return tbk_nocoupon_msg;}var mapDataResponse = taobaoCommonApi.MaterialOptional(keyMatch.Value.Split('-')[0]);string responeMessage = "";float numid = float.Parse(item_id);if (mapDataResponse!=null&&mapDataResponse.Count > 0){foreach (var g in mapDataResponse){if (g.NumIid == numid){if (string.IsNullOrEmpty(g.CouponInfo)){var hongbao = decimal.Parse(g.ZkFinalPrice) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【约返利】{Math.Round(hongbao, 2)}元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(g.Url, g.PictUrl + "_400x400.jpg")}\n==========================\n下单确认收货后就能收到返利佣金啦~\n 点击查看  <a href='http://mp.weixin.qq.com/s?__biz=Mzg2NTAxOTEyMA==&mid=100000146&idx=1&sn=62405c8df3db46e74940aefb9ac3737b&chksm=4e61340d7916bd1bf645afbc6d10c1f19561d7fa59847516c01e64c0791e6d544f4f56c4f498#rd'>如何领取返利</a>";return responeMessage;}else{var hongbao = (decimal.Parse(g.ZkFinalPrice) - decimal.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(g.CommissionRate) / 10000 * commission_rate;responeMessage = $"{g.Title}\n【在售价】{g.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(g.ZkFinalPrice) - double.Parse(Regex.Match(g.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(g.CouponShareUrl, g.PictUrl + "_400x400.jpg")}\n";return responeMessage;}}}//没有找到,有相似宝贝推荐var w = mapDataResponse.Where(y => !string.IsNullOrEmpty(y.CouponId)).OrderByDescending(y => y.Volume).FirstOrDefault();if (w == null){responeMessage = ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");}else{var hongbao = (decimal.Parse(w.ZkFinalPrice) - decimal.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value)) * decimal.Parse(w.CommissionRate) / 10000 * commission_rate;responeMessage = $"/:rose 亲,这款商品的优惠返利活动结束了~\n已为你推荐以下宝贝。\n==========================\n{w.Title}\n【在售价】{w.ZkFinalPrice}元\n【巻后价】{Math.Round(double.Parse(w.ZkFinalPrice) - double.Parse(Regex.Match(w.CouponInfo, "减" + @"(\d+)").Groups[1].Value), 2)} 元\n复制这条信息,打开「手机绹宝」领巻下单{taobaoCommonApi.GetTaobaoKePassword(w.CouponShareUrl, w.PictUrl + "_400x400.jpg")}\n";}return responeMessage;}else{return ConfigurationManager.AppSettings["tbk_nocoupon_msg"].Replace("\\n", "\n").Replace("\\ue231", "\ue231");}}

详细请查看 http://blog.yshizi.cn/5.html
请关注“网购有券”,支持下我吧。有任何问题也可以在公众号中联系我。

c# 通过手淘分享查询淘宝优惠券相关推荐

  1. 微信公众号开发淘宝优惠券查询的思路和原理

    做微信公众号查券首先需要一个1个产品库,我们可以使用淘宝的开放平台提供的产品库通用物料搜索API. 通过用户手淘分享的商品信息,信息中包含商品标题和链接还有淘口令,我们可以用正则表达式提取出商品标题 ...

  2. uniapp、uniCloud实现微信公众号自动查询淘宝京东优惠券制作过程

    uniapp.uniCloud实现微信公众号自动查询淘宝京东优惠券制作过程 微信公众号自动查询淘宝京东优惠券机器人制作教程.服务器通过uniapp提供的uniCloud云服务搭建,建议使用阿里云,不要 ...

  3. 淘宝购物折扣秒杀分享群淘宝红包怎么抢

    天猫秒杀优惠群,淘宝红包秒杀群![特价秒杀群号(286578730),中毒太深嘞 自从加了这个QQ群,就停不下来了!不过还好价格都不贵..] 很多朋友会有很大的疑问,QQ上的特价秒杀优惠券群是否可信? ...

  4. 淘宝号标签,,猜你喜欢推荐,消费潜力值,淘宝号的千人千面,购物足迹,潜在购买类目,淘宝号的潜在成交词,官方推荐的搜索词,淘宝标签查询,淘宝号是否打上标签,标签透视,标签接口,猜你喜欢接口,

    简介: 可以查询到指定淘宝号被淘宝推荐的搜索词,和猜你喜欢的词库和商品id库. 淘宝标签查询基于官方千人千面算法推荐,针对不同的消费者推送不同的潜在成交商品和可消费的金额. 实时查询买家曾浏览过.购买 ...

  5. 淘宝/天猫店铺oAuth2.0接口:Taobao.user.seller.get 查询淘宝卖家用户信息接口接入说明

    淘宝/天猫店铺oAuth2.0接口:Taobao.user.seller.get 查询淘宝卖家用户信息接口接入说明: 为了进行电商平台淘宝的API开发,首先我们需要做下面几件事情: 1)开发者注册一个 ...

  6. 淘宝/天猫店铺oAuth2.0订单同步接口:Taobao.user.seller.get 查询淘宝卖家用户信息接口接入说明

    淘宝/天猫店铺oAuth2.0接口:Taobao.user.seller.get 查询淘宝卖家用户信息接口接入说明: 为了进行电商平台淘宝的API开发,首先我们需要做下面几件事情: 1)开发者注册一个 ...

  7. 【报告分享】淘宝直播2021年度报告-淘榜单淘宝直播(附下载)

    摘要:相较于传统品牌,新品牌在新渠道的尝试更为大胆,在淘宝直播里的投入力度也普遍更大.他们诞生于内容电商时代,善于快速尝试.磨合.建立适宜品牌发展的达人矩阵.除此之外,它们也积极尝试自播渠道,从中获取 ...

  8. 用微信公众号做淘宝优惠券查券和返利机器人的详细配置教程

    用微信公众号做淘宝优惠券查券和返利机器人的详细配置教程:微信公众号淘宝客机器人,微信公众号淘客系统自助搭建教程 一.淘宝联盟官方淘宝客私域渠道ID申请 1.登陆淘宝联盟 用需要开通淘宝联盟的手机淘宝扫 ...

  9. 分享在淘域网申请域名的步骤

    为什么80%的码农都做不了架构师?>>>    鉴于有部分用户对淘域网域名交易平台的域名注册流程不熟悉,本站特整理出一份最新的 申请域名 步骤,有图有真相. 第一步,登陆淘域网首页, ...

最新文章

  1. 智源伍昱:被AI“耽误”的文艺青年,用技术对抗偏见
  2. Java 定义字符串数组
  3. 用Maven构建Mahout项目
  4. 【maven】仓库的优先级顺序
  5. The requested URL /nagios/cgi-bin/statusmap.cgi was not found on this server
  6. 世界上没有后悔药,时间匆匆,从关注它们开始......
  7. Vue-easyui中如何给ComboGrid添加过滤器
  8. 双向板课设按照弹性计算_T004 结构专业施工图技术问答结构布置与计算
  9. php 数组中连续的数字,php数组中最近的次要数字
  10. USB接口、串口、并口有何区别
  11. java 绘制点阵_[Java基础知识]点阵字库在JAVA中的实现
  12. 互联网晚报 | 06月08日 星期三 | ​教育部回应高考试题疑泄露;​上海落户新规;字节跳动考虑出售得物少数股份...
  13. 点击电脑版微信一直打不开解决方案
  14. 《Java编程十五讲》第十一讲:脚本
  15. 解决非root用户没有权限运行docker命令的问题
  16. Wannafly挑战赛29-御坂美琴(递归模拟)
  17. 【ROM制作工具】如何美化ROM、集成高级设置等特色功能到刷机包教程
  18. [乡土民间故事_徐苟三传奇]第三回_小苟三巧施放鱼计
  19. AMD天下!10款超性价比S754闪龙主板
  20. 1.3.3 什么是好莱坞法则

热门文章

  1. 用python对股票期货做时序分析
  2. Android licenses not accepted
  3. Bind9源代码分析
  4. Android 11 新特性和API兼容
  5. Python教程视频千锋最新版免费分享
  6. 涛思数据创始人陶建辉荣获“2020中国开源杰出贡献人物”奖
  7. 拯救rm-rf删库事故
  8. MiniGUI移植过程
  9. VS1003调试例程
  10. linux dd 硬盘克隆,如何使用Linux dd命令克隆磁盘