-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGet_promo_codes.py
44 lines (33 loc) · 1.06 KB
/
Get_promo_codes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding:utf-8 -*-
# Author: 搬瓦工优惠网(bwgyhw.com)
# Date: 2018/8/16
# Time: 18:46
import csv
import re
import time
import requests
BWH_PROMO_CODE_URL = 'https://bwh1.net/cart.php?a=add&pid=90'
BWH_CHECK_DISCOUNT_URL = 'https://bwh1.net/cart.php?a=view'
CODES = []
def regex_promo_code():
code_html = requests.post(BWH_PROMO_CODE_URL).text
code = re.search(r'Try this promo code: (\w*)', code_html)
print(code[1])
return code[1]
def check_promo_code(code):
data = {'promocode': code}
check_html = requests.post(BWH_CHECK_DISCOUNT_URL, data).text
discount = re.search(r'- ([\d\.]+)%', check_html)
return discount[0]
def save_to_file(code, discount):
with open('codes.csv', "a") as file:
writer = csv.writer(file)
writer.writerow([code, discount])
if __name__ == "__main__":
while True:
g_code = regex_promo_code()
g_discount = check_promo_code(g_code)
if g_code not in CODES:
CODES.append(g_code)
save_to_file(g_code, g_discount)
time.sleep(5)