-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstrumentRequest.py
218 lines (161 loc) · 5.41 KB
/
InstrumentRequest.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# coding: utf-8
# In[84]:
from PIL import Image
import pytesseract
import sys
import os
import heapq
import random
import re
import requests
import json
import time
import concurrent.futures
# In[129]:
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Host' : 'dxyq.njust.edu.cn',
'Proxy-Connection' : 'keep-alive',
'Referer' : 'http://dxyq.njust.edu.cn/norderday.jsp?equipId=4af7d10b44723d690144726efb330007&time=1543507200000&ro=false',
'Origin' : 'http://dxyq.njust.edu.cn',
'Accept' : 'application/json, text/javascript, */*',
'Accept-Language' : 'zh-CN,zh;q=0.9',
'Accept-Encoding' : 'gzip, deflate'
}
proxies = {
'http': 'http://xzproxy.cnsuning.com:8080',
'https': 'https://xzproxy.cnsuning.com:8080'
}
getImageHeaders = {
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Upgrade-Insecure-Requests' : '1',
'Proxy-Connection' : 'keep-alive',
'Pragma' : 'no-cache',
'Accept-Language' : 'zh-CN,zh;q=0.9',
'Accept-Encoding' : 'gzip, deflate',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Host' : 'dxyq.njust.edu.cn'
}
cookies = {
'JSESSIONID' : 'C443724173F1ED93C976D7C8A314A722',
'iPlanetDirectoryPro' : 'dk3Jfhy2rQCSKbDuqbg6bt',
'MOD_AUTH_CAS' : 'MOD_AUTH_ST-229670-BKcMOTddIlqhgtdT9WPB1543304275052-bhIs-cas',
}
params = {
'equipId' : '4af7d10b44723d690144726efb330007',
'startDate' : '2018-12-4 19:00',
'endDate' : '2018-12-4 20:00',
'content' : 'testtesttesttesttest',
'noMerge' : 'false',
'operatorType' : '1',
'userno' : '',
'validate' : ''
}
errorMap = {
'82 79' : '8279',
'444a' : '4448',
'43 72' : '4372',
'93 76' : '9376'
}
#二值化,输入阈值和文件地址
def binaryzation(threshold,image_address):
image=Image.open(image_address)#打开图片
image=image.convert('L')#灰度化
table=[]
for x in range(256):#二值化
if x<threshold:
table.append(0)
else:
table.append(1)
image=image.point(table,'1')
return image
def main():
while(True):
if time.strftime("%M", time.localtime(time.time())) == '00' or time.strftime("%M", time.localtime(time.time())) == '59':
submitRequests()
submitRequests()
submitRequests()
submitRequests()
submitRequests()
submitRequests()
def submitRequests():
image = requests.get('http://dxyq.njust.edu.cn/image', cookies=cookies, headers = getImageHeaders, proxies=proxies)
validateNum = getImageNumStr(image)
validateNum = twiceCorrect(validateNum)
params['validate'] = validateNum
print(params)
response = requests.post('http://dxyq.njust.edu.cn/ajax/orderSave.action', cookies=cookies, headers=headers, data=params, proxies=proxies)
response.encoding = 'utf-8'
content = json.loads(response.content)
res = content['result']['result']
message = content['result']['message']
print('%s: 申请结果:%s, 信息:%s' % (time.strftime("%H:%M:%S", time.localtime(time.time())), res, message))
def twiceCorrect(src):
return errorMap.get(src, src)
def getNextImageCount():
baseImagePath = 'D:/images'
imageFileNames = os.listdir(baseImagePath)
pattern = re.compile('(\d+)\.jpg')
maxNum = -1
for imageFile in imageFileNames:
match = re.findall(pattern, imageFile)
#print(imageFile)
num = int(match[0])
if num > maxNum:
maxNum = num
return maxNum + 1
def transHundersImages():
basePath = 'D:/images/%d.jpg'
errorCount = 0
errorList = []
errorRes = []
for i in range(1, 350):
imagePath = basePath % (i)
if not os.path.exists(imagePath):
continue
image = binaryzation(141, imagePath)
result = pytesseract.image_to_string(image)
result = twiceCorrect(result)
print('%-5dresult: %s' % (i, result))
if len(result) != 4 or not result.isdigit():
errorCount += 1
image.show()
errorList.append(i)
errorRes.append(result)
print('错误个数为: %d' % (errorCount))
print('错误的图为:', errorList)
print('错误的结果为:', errorRes)
def getImageNumStr(response):
image = response.content
num =getNextImageCount()
path = 'D:/images/%d.jpg' % num
with open(path, 'wb') as f:
f.write(image)
image=binaryzation(141, path)
result=pytesseract.image_to_string(image)
print('图片写入D:/images成功,路径为%s, 识别图片验证码为%s' % (path, result))
return result
#main()
#transHundersImages()
#getNextImageCount()
# In[130]:
main()
# In[7]:
#'D:/images/%d.jpg' % 3
# In[19]:
#os.listdir('D:/images')
# In[27]:
#[random.randint(0, 100) for i in range(20)]
# In[119]:
#pattern = 'D:/images/(\d+)\.jpg'
#result = re.findall(pattern, 'D:/images/311.jpg')
#result[0]
# In[69]:
#image = requests.get('http://dxyq.njust.edu.cn/image', cookies=cookies, headers = getImageHeaders, proxies=proxies)
#getImagePath(image)
# In[67]:
#result = requests.get('http://www.baidu.com', verify = False)
#result.encoding = 'utf-8'
#result.status_code
# In[102]:
#'12f0'.isdigit()