简易百度OCR

1
2
3
pip install baidu-aip #安装SDK
111.202.114.49 console.bce.baidu.com #改善宿舍的联通网络体验,hosts定向到最近的联通服务器
123.125.114.17 aip.baidubce.com
1
2
3
4
5
6
7
8
9
10
11
12
13
from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"

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
一个简单的应用, 去掉了关键信息,想使用请自行补全
from aip import AipOcr
import requests, time, re, random

space = re.compile(r'\s+')
htag = re.compile(r'<[^>]+>')

""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"

def BaiduOCR(image, options=options):
if isinstance(image, str):
results = client.basicGeneralUrl(image, options)
else:
results = client.basicGeneral(image, options)
if 'error_code' in results:
print (f'BaiduOCR error {results["error_code"]} {results["error_msg"]}')
return ''
return ''.join(line['words'] for line in results['words_result'] if 'words' in line)

def jsonfy(s:str)->object:
#此函数将不带双引号的json的key标准化
assert s[0] in ('{','['), print(s) or 'jsonfy 登录失效,请重新获取Cookie!'
obj = eval(s, type('js', (dict,), dict(__getitem__=lambda s, n: n))())
return obj

def getScLc(d):
return (int(d['sc']), int(d['lc']))

def getXkList(r):
a = r.text.find('[')
b = r.text.find(r';/*sc 当前人数, lc 人数上限*/')
c = r.text.find('{', b)
j = jsonfy(r.text[a:b])
j2 = jsonfy(r.text[c:])
return [(one['id'], one['no'], one['name'], getScLc(j2[str(one['id'])])) for one in j]

def getData(Form = None):
if Form:
r = requests.post('', data = Form, headers=headers)
else:
r = requests.get(r'', headers=headers)

print(f'getData status_code {r.status_code}')
return r

def getTimestamp(size = 1000):
t = time.time()
return int(round(t * size))

def getCaptcha():
r = requests.get(f'', headers=headers)
print(f'getCaptcha status_code {r.status_code}')
img = r.content
text = BaiduOCR(img)
print(f'getCaptcha results {text}')
return text.replace(' ','')

def Xk(CourseId):
data = {
"optype": "true",
"operator0": "",
"captcha_response": ""
}
r = requests.post(r'', data = data, headers=headers)
print(f'Xk status_code {r.status_code}')
return htag.sub(' ', space.sub('', r.text))

def Xk2S(CourseId):
for i in range(3):
st = Xk(CourseId)
if '选课成功' in st:
print(f'Xk2S 第{i+1}次选课成功!')
return True
else:
print(f'Xk2S 第{i+1}次选课:')
print(st)
assert '选课失败:公选人数已满' not in st, 'Xk2S 公选人数已满 无法继续'
assert '选课失败:你已经选过' not in st, 'Xk2S 选课成功 无需继续'
time.sleep(random.randint(2,5))
else:
return False

Form = {
"lessonNo": "",
"courseCode": "",
"courseName": ""
}

headers = {
"Accept": "image/webp,image/apng,image/*,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Connection": "keep-alive",
"Cookie": "",
"Host": "",
"Referer": "",
"User-Agent": ""
}

def getCourse():
Courses = getXkList(getData(Form))
print (Courses)
for Course in Courses:
if Course[1] == Form['lessonNo']:
print (Course)
return Course
print('getCourse 出现未知错误,请重试!')

def isSuccessful():
Courses = getXkList(getData())
print (Courses)
for Course in Courses:
if Course[1] == Form['lessonNo']:
print (Course)
return True
return False


简易百度OCR
https://b.limour.top/150.html
Author
Limour
Posted on
June 18, 2020
Licensed under