-
Notifications
You must be signed in to change notification settings - Fork 0
/
bawangcan.py
197 lines (183 loc) · 6.51 KB
/
bawangcan.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import requests
import json
import re
import io
import xlwt
import datetime
import configparser
import os
from tqdm import tqdm
import random
def getBranchStore(activity):
cookies = {
'dper': dper
}
url = 'https://m.dianping.com/mobile/dinendish/apply/getPreApply' # 分店信息
headers = {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
}
data = {
'activityId': activity,
'env': 1,
'token': "*",
}
response = requests.post(
url=url, headers=headers, cookies=cookies,data=json.dumps(data))
if response.status_code == 200:
try:
# 弱水三千,我只取一瓢
branchId = random.choice(
response.json()['data']['branchs'])["branchId"]
except:
branchId = None
return branchId
#从config.ini中读取dper和cityId
cf = configparser.ConfigParser()
path = os.path.abspath('config.ini')
cf.read(path)
dper = cf.get('basic', 'dper')
cityId = cf.get('basic','cityId')
# 创建一个workbook
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('霸王餐')
row0 = ["序号","霸王餐名称","报名状态","报名地址","异常原因"]
for i in range(0,len(row0)):
worksheet.write(0,i,row0[i])
orgin_url = 'http://s.dianping.com/event/'
#获取霸王餐列表
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Referer': 'http://s.dianping.com/event/beijing',
'Origin': 'http://s.dianping.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
'Content-Type': 'application/json',
}
ids = []
activityTitles = []
types_food = [0,1, 2, 6, 99]
types_all = [0]
data = {"cityId":cityId,"type":0,"mode":"","page":1}
for t in types_food:
for page in range(1,15):
data["page"] = str(page)
data["type"] = str(t)
# print(data)
response = requests.post('http://m.dianping.com/activity/static/pc/ajaxList', headers=headers, data=str(json.dumps(data)))
# print(page)
for item in response.json()['data']['detail']:
activityTitles.append(item['activityTitle'])
ids.append(item['offlineActivityId'])
# print(str(page) + ":" + item['activityTitle'])
# print(ids)
print('搜索到'+str(len(ids))+'条霸王餐')
cookies = {
'dper':dper
}
#报名霸王餐
headers = {
'Origin': 'http://s.dianping.com',
'Accept-Encoding': 'gzip, deflate',
'X-Request': 'JSON',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8;',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Accept': 'application/json, text/javascript',
'Referer': 'http://s.dianping.com/event/1063463422',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
}
data = {
'offlineActivityId': '1063463422',
'phoneNo': '180****3715',
'shippingAddress': '',
'extraCount': '',
'birthdayStr': '',
'email': '',
'marryDayStr': '',
'babyBirths': '',
'pregnant': '',
'marryStatus': '0',
'comboId': '1',
'branchId': 2,
'usePassCard': '0',
'passCardNo': '',
'isShareSina': 'false',
'isShareQQ': 'false'
}
def set_color(color,bold):
style=xlwt.XFStyle()
font=xlwt.Font()
font.colour_index=color
font.bold = bold
style.font=font
return style
success = []
successed = []
fail = []
count = 1
for _id in tqdm(ids):
text = requests.get(orgin_url+str(_id),headers=headers,cookies=cookies).text
# print(text)
# print(str(_id))
shopid = re.search(r'shopid:.*',text).group()
# print(shopid)
shopid = shopid.split(':')[1].split(',')[0]
# print(shopid)
# print(shopid)
# print("shopid")
# print(shopid)
with open('file.txt','a',encoding='utf-8') as f:
f.write(text)
data['offlineActivityId'] = str(_id)
# print(getBranchStore(data['offlineActivityId']))
result = getBranchStore(data['offlineActivityId'])
print(result)
# data['pregnant'] = 'one'
data['branchId'] = str(result)
# data['shopuuid'] = shopid
# print(data['branchId'])
print(data)
response = requests.post('http://s.dianping.com/ajax/json/activity/offline/saveApplyInfo', headers=headers,cookies=cookies, data=data)
with open('result.txt','a',encoding='utf-8') as f:
f.write(response.text)
msg = json.loads(response.text)
print(msg)
if "不要重复报名" in msg["msg"]["html"]:
successed.append(activityTitles[ids.index(_id)])
worksheet.write(count,0,count)
worksheet.write(count,1,activityTitles[ids.index(_id)])
worksheet.write(count,2,"已报名过")
worksheet.write(count,3,"http://s.dianping.com/event/"+str(_id))
worksheet.write(count, 4, msg["msg"]["html"])
elif "报名成功" in msg["msg"]["html"]:
success.append(activityTitles[ids.index(_id)])
worksheet.write(count,0,count)
worksheet.write(count,1,activityTitles[ids.index(_id)])
worksheet.write(count,2,"报名成功")
worksheet.write(count,3,"http://s.dianping.com/event/"+str(_id))
worksheet.write(count, 4, msg["msg"]["html"])
else :
# print(msg)
fail.append(activityTitles[ids.index(_id)])
worksheet.write(count,0,count)
worksheet.write(count,1,activityTitles[ids.index(_id)])
worksheet.write(count,2,"报名异常",set_color(0x02,True))
worksheet.write(count,3,"http://s.dianping.com/event/"+str(_id))
worksheet.write(count,4,msg["msg"]["html"])
count=count+1
worksheet.write(0,5,'成功登记'+str(len(success))+'个活动')
worksheet.write(1,5,'已经登记过'+str(len(successed))+'个活动')
worksheet.write(2,5,'登记异常'+str(len(fail))+'个活动')
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H`%M`%S')#现在
workbook.save('霸王餐'+nowTime+'.xls')
print('成功登记'+str(len(success))+'个活动')
# for i in success:
# print(i)
print('已经登记过'+str(len(successed))+'个活动')
# for i in successed:
# print(i)
print('登记异常'+str(len(fail))+'个活动')
# for i in fail:
# print(i)