自动获取搬瓦gong最新优惠码脚本

介绍

搬瓦gong优惠码信息是隐藏在具体商品购买页面的,所以整个的脚本流程可以分为两步:

  1. 在商品页面正则出这个隐藏的优惠码
  2. 在购买页面测出这个优惠码的优惠力度

原因是执行https://github.com/flyzy2005/get_bwh_promo_codes脚本失败,原来是https://bwh1.net打不开了,变成了https://bwh88.net 所以更新了以下脚本。

参考

https://www.bwgyhw.cn/bandwagonhost-how-to-get-latest-promo-codes/

https://github.com/flyzy2005/get_bwh_promo_codes

脚本

# -*- coding:utf-8 -*-

import csv
import re
import time
import requests
import sys

BWH_PROMO_CODE_URL = 'https://bwh88.net/cart.php?a=add&pid=44'
BWH_CHECK_DISCOUNT_URL = 'https://bwh88.net/cart.php?a=view'
BWH_vps_hosting = 'https://bwh88.net/vps-hosting.php'
CODES = []

def logo():
    print("""
  _____ _             _        _____      _   
 / ____| |           | |      / ____|    | |  
| (___ | |_ _   _  __| |_   _| |     __ _| |_ 
 \___ \| __| | | |/ _` | | | | |    / _` | __|
 ____) | |_| |_| | (_| | |_| | |___| (_| | |_ 
|_____/ \__|\__,_|\__,_|\__, |\_____\__,_|\__|
                         __/ |                
                        |___/     
                         
            https://www.cnblogs.com/StudyCat/
             
    """)

def regex_promo_code():
    try:
        Default_Header = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                          'Referer': BWH_vps_hosting,
                          'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'
                          }        
        r = requests.get(BWH_PROMO_CODE_URL, headers=Default_Header)
        if r.status_code == 200:
            code_html = r.text
            code = re.search(r'Try this promo code: (\w*)', code_html)
            return code[1]
        else:
            return
    except Exception as e:
        print(e)
        return


def check_promo_code(code):
    try:
        data = {'promocode': code}
        Default_Header = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                          'Referer': BWH_PROMO_CODE_URL,
                          'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'
                          }           
        r = requests.post(BWH_CHECK_DISCOUNT_URL, headers=Default_Header, data=data)
        if r.status_code == 200:
            check_html = r.text
            discount = re.search(r'- ([\d\.]+)%', check_html)
            return discount[0]
        else:
            return
    except Exception as e:
        print(e)
        return

def save_to_file(code, discount):
    with open('codes.csv', "a") as file:
        writer = csv.writer(file)
        writer.writerow([code, discount])     

if __name__ == "__main__":
    logo()
    try:
        while True:
            g_code = regex_promo_code()
            if g_code:
                g_discount = check_promo_code(g_code)
                if g_discount and g_code not in CODES:
                    CODES.append(g_code)
                    save_to_file(g_code, g_discount)
                    print("Promocode: %s    Discount: %s" % (g_code, g_discount))
            time.sleep(5)
    except KeyboardInterrupt:
        print("Caught KeyboardInterrupt, quitting...")
        sys.exit(1)    

  

posted @ 2020-02-02 14:15  StudyCat  阅读(301)  评论(0编辑  收藏  举报