获取淘宝优惠券

经常看到别人在淘宝买东西可以领到好多优惠券,我也想要啊,于是搜刮全网信息,各种百度,终于梦想实现了,哈哈哈哈。。。好开心,今天分享给大家。

代码如下:

import json
import requests


class YouHuiQuan():
    def __init__(self, old_code):
        """
        :param old_code: 原始物品口令
        """
        # 获取物品信息的URL
        self.info_url = "http://mgi.sitezt.cn/api/i/cmsitem/cmsqwsearch"
        # 获取物品优惠券的URL
        self.code_url = "http://mgi.sitezt.cn/api/b/buy"
        # 请求头
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding": "gzip, deyhqate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Host": "mgi.sitezt.cn",
            "Pragma": "no-cache",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
        }
        # 原始物品口令
        self.old_code = old_code

    def get_info(self):
        params = {
            "param.sort": "0",
            "param.page": "1",
            "param.pageSize": "100",
            "param.keyWord": self.old_code
        }
        # 发送请求
        resp = requests.get(self.info_url, params=params, headers=self.headers)

        # 判断是否正确返回
        if resp.status_code != 200:
            return False
        else:
            content = resp.text
            content_dict = json.loads(content)["PData"][0]
            # print(content_dict)
            info_dict = {}
            info_dict["num_iid"] = content_dict["num_iid"]
            info_dict["title"] = content_dict["title"]
            info_dict["zk_final_price"] = content_dict["zk_final_price"]
            info_dict["coupon_info"] = content_dict["coupon_info"]
            info_dict["pict_url"] = content_dict["pict_url"]
            info_dict["short_title"] = content_dict["short_title"]
            return info_dict

    def get_code(self, id):
        # url = "http://mgi.sitezt.cn/api/b/buy"
        params = {
            "Dto.rId": "2318806650",
            "Dto.inviteCode": "ZC4V7HY",
            "Dto.shortInviteCode": "",
            "Dto.pId": "",
            "Dto.userToken": "",
            "Dto.domain": "7gv.7fw48.cn",
            "Dto.itemId": id,
            "Dto.needInviteCode": "true",
            "Dto.againLess": "true",
            "Dto.newUserInfo": "true",
            "Dto.itemInfo": "true",
            "Dto.url": "true",
            "Dto.kL": "true",
            "Dto.shortUrl": "true",
            "Dto.downloadGuide": "true"
        }

        resp = requests.get(self.code_url, params=params, headers=self.headers)

        # 判断是否正确返回
        if resp.status_code != 200:
            return False
        else:
            content = resp.text
            code_dict = {}
            code_dict["KL"] = json.loads(content)["KL"]
            code_dict["Recommend"] = json.loads(content)["ItemInfo"]["Recommend"]
            code_dict["Price"] = json.loads(content)["ItemInfo"]["Price"]
            code_dict["QuanAmount"] = json.loads(content)["ItemInfo"]["QuanAmount"]
            return code_dict


if __name__ == '__main__':
    old_code = input("请输入淘宝口令:")
    yhq = YouHuiQuan(old_code)
    info_dict = yhq.get_info()
    code_dict = yhq.get_code(info_dict["num_iid"])

    Price = round(code_dict["Price"] / 100, 2)
    QuanAmount = round(code_dict["QuanAmount"] / 100, 2)
    message = "%s\n【原价】:%s元\n【优惠券】:%s元\n【券后】:%s元\n--------------------\n长按赋值%s到Taobao买" % (
    info_dict["title"], Price, QuanAmount, round(Price - QuanAmount, 2), code_dict["KL"])

    print(message)

运行结果:
获取淘宝优惠券