青少年普法网每年的题还都不一样,真麻烦。每年都要重新写的么?
使用方法:
用Excle保存三列数据。然后另存为"321.csv"
用户名1,姓名1,密码1
用户名2,姓名2,密码2
用户名3,姓名3,密码3
……
然后就启动脚本。
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2024-9-30 21:03:11
# @Author : Xzap (xzap@163.com)
# @Link : https://xzap.github.io
# @Version : 3.0
from playwright.sync_api import Playwright, sync_playwright, expect
import time
import requests
import ddddocr
import random
import csv
ocr = ddddocr.DdddOcr(show_ad = False)
def shuashua(uid, name, passwd):
taskid = 25
passwd_all = "242465"
page = context.new_page()
page.goto("https://static.qspfw.moe.gov.cn/user/#/user/login")
time.sleep(1)
page.get_by_text("学校统一学习入口").click()
page.get_by_placeholder("请输入学校统一学习码").fill(passwd_all)
page.get_by_placeholder("请输入学习账号").fill(uid)
page.get_by_placeholder("请输入姓名").fill(name)
page.get_by_label("我已阅读并同意").check()
page.get_by_role("img", name="换一张").screenshot(animations="disabled" , path="pw_cap.png")
with open("pw_cap.png","rb") as f:
image = f.read()
ocr.set_ranges("0123456789abcdefghijklmnopqrstuvwxyz")
result = ocr.classification(image, probability=True)
# print(result)
res = ""
for j in result['probability']:
res += result['charsets'][j.index(max(j))]
# print (res)
if len(res) == 6 :
res = res[1:]
print(f"验证码: {res}")
page.get_by_placeholder("请输入验证码").fill(res)
page.get_by_role("button", name="登 录").click()
time.sleep(1)
ck = page.context.cookies()
s = requests.session()
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
"source": "1",}
s.headers.update(headers)
for cookie in ck:
s.cookies.set(cookie['name'],cookie['value'])
s.headers["Access-Token"] = s.cookies['SESSION']
c = s.get("https://api.qspfw.moe.gov.cn/examApi/practice/getUserProcess?taskId=25")
process = c.json()['data']['process']
grade = c.json()['data']["gradeName"]
print(grade)
if process == "100%":
print ("已完成")
else :
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/practice/getColumnList?taskId={taskid}")
cids = c.json()["data"]["columnList"]
for cl in cids:
cid = cl["columnId"]
print(cl["name"])
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/paper/getExamInfo?taskId={taskid}")
c = s.get(f"https://api.qspfw.moe.gov.cn/punchApi/punch/activity/selectActivityByUser?taskId={taskid}")
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/practice/studyByChapterId?columnId={cid}&taskId={taskid}")
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/practice/getColumnList?taskId={taskid}")
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/practice/studyByColumnId?columnId={cid}&taskId={taskid}")
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/study/practice?columnId={cid}&taskId={taskid}&takeTime={random.randint(60,180)}")
if grade == "三年级" or grade == "四年级" or grade == "五年级" or grade == "六年级" :
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/practice/practice?columnId={cid}&taskId={taskid}")
tiku = c.json()["data"]["questionBankList"]
aa = {}
for i in tiku:
aa[i["answerOptions"]] = i["answer"]
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/paper/getPaper?taskId={taskid}")
tiku2 = c.json()["data"]["paper"]["paperInfo"]
pid = c.json()["data"]["paper"]["id"]
rid = c.json()["data"]["resultId"]
answer = ""
for j in tiku2:
answer += f'{j["id"]}_{aa[j["answerOptions"]]}@!@'
# print (f"https://api.qspfw.moe.gov.cn/examApi/paper/saveResult?taskId={taskid}&paperId={pid}&resultId={rid}&answers={answer}&takeTime={random.randint(100,280)}")
c = s.get(f"https://api.qspfw.moe.gov.cn/examApi/paper/saveResult?taskId={taskid}&paperId={pid}&resultId={rid}&answers={answer}&takeTime={random.randint(100,280)}")
# print (c.json())
c = s.get("https://api.qspfw.moe.gov.cn/examApi/practice/getUserProcess?taskId=25")
print(c.json()['data']['process'])
c = s.get("https://api.qspfw.moe.gov.cn/userAuthApi/user/logout")
print (c.json()["data"])
# page.goto("https://static.qspfw.moe.gov.cn/user/#/user/login")
# time.sleep(1)
page.close()
with sync_playwright() as playwright:
# 'chromium' Or 'firefox' or 'webkit'.
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
students = []
with open("321.csv","r") as csvfile:
cf = csv.reader(csvfile)
for row in cf:
students.append(row)
allnum = len(students)
for num, ss in enumerate(students):
stime = time.time()
print ("="*50)
print (f"{num+1} / {allnum} {ss[1]}")
att = 0
success = False
# shuashua(ss[0], ss[1], ss[2])
while att < 3 and not success :
try :
shuashua(ss[0], ss[1], ss[2])
success = True
except Exception as e:
print (e)
att += 1
time.sleep(3)
if att == 3 :
with open ("fail.txt", "a", encoding="utf-8") as f :
f.write(",".join(ss) + "\n")
print (f"{num+1} {ss[1]} fail!")
break
else :
print ("Retry...")
endtime = time.time()
ttime = endtime - stime
print (f"耗时{ttime:.2f}秒。")
context.close()
browser.close()
|