該源碼主要利用python+selenium來進行爬取題庫和自動答題。
主要思路和代碼步驟如下:
首先打開網址:https://static.qspfw.com/xf2020/learn_practice_list.html
然后找到登錄按鈕,同時按下shift+ctrl+i,打開控制台,找到登錄按鈕的位置信息,然后復制xpath,以備用
<ignore_js_op>
點擊 登錄按鈕后跳轉到登錄頁面
<ignore_js_op>
同理,找到 用戶名,密碼和登錄的xpath信息,以備用
處理彈窗
<ignore_js_op>
登錄后找到 在線學習的xpath
<ignore_js_op>
之后找到待學習的課程等過程及xpath就不再贅述,方法相同,一步步尋找
爬取題庫
進入答題頁面,可以同理找到 問題的xpath和選項的xpath
<ignore_js_op>
選擇任意一個選項,等待頁面刷新,正確選項會變成綠色,且class變成‘item success’,
剛才選擇的錯誤選項的class變成‘item error‘,
因此可以根據class的變化獲得正確答案
【嘗試獲取class='item success'但失敗了,還未尋找到解決辦法,故采用另一種方法】
選擇A后,采集選項的class值,如果是‘item error’的話就跳過,選擇B,當不是‘item error’時就是正確答案。
【耗費時間比直接獲取class=‘item success’多3倍】
然后選擇下一題依次答題的xpath,答題結束后選擇 學習下一主題的xpath進行跳轉
將采集到的題目和正確選項答案保存到文件中,題庫爬取完成
將題庫導入到程序中,然后進入答題頁面
題庫格式為【題目/A/選項】
獲得題目的xpath位置,找到其文本信息,將該文本和題庫列表進行搜索
當題庫中有該題目時,確定其索引位置,正確答案的索引值為其值+1,獲得正確答案
然后進行判斷,根據正確答案,選擇要選擇選項的xpath,然后進行選擇,
完成該題目后選擇下一題,
全部完成后提交。
關閉提交后的彈窗。
源碼如下:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
|
import
time
#導入時間庫
from
selenium
import
webdriver
""" 賬號密碼 """
name
=
"賬號"
password
=
"密碼"
"""網址"""
url
=
"https://static.qspfw.com/xf2020/learn_practice_list.html"
chromedriver
=
'chromedriver.exe'
#瀏覽器內核位置
chome_options
=
webdriver.ChromeOptions()
wd
=
webdriver.Chrome(chromedriver, chrome_options
=
chome_options)
#瀏覽器
wd.delete_all_cookies()
# 刪除cookies
wd.maximize_window()
# 將瀏覽器最大化
wd.implicitly_wait(
10
)
#網頁無響應或找不到標簽等,最長嘗試10秒
wd.get(url)
element
=
wd.find_element_by_xpath(
'//*[@id="login"]'
)
#登錄按鍵
element.click()
element
=
wd.find_element_by_xpath(
'//*[@id="username"]'
)
#用戶名
element.send_keys(name)
element
=
wd.find_element_by_xpath(
'//*[@id="password"]'
)
#密碼
element.send_keys(password)
time.sleep(
3
)
#延遲3秒
element
=
wd.find_element_by_xpath(
'/html/body/div[2]/div/form/a'
)
#登錄
element.click()
#點擊
time.sleep(
2
)
#延遲2秒
#有時候會出現彈窗,顯示 服務器忙或登錄成功等彈窗,需要關閉
"""關閉瀏覽器彈窗"""
try
:
a
=
wd.switch_to.alert
# 切換alert
print
(a.text)
# 獲取彈窗上的文本
a.accept()
# 確認,相當於點擊[確定]按鈕
# a.dismiss() # 取消,相當於點擊[取消]按鈕
except
:
pass
#有時頁面會出現“服務器錯誤”彈窗,關閉后再次登錄才行,如果不需就注釋掉
# """再次登錄"""
# try:
# element = wd.find_element_by_xpath('/html/body/div[2]/div/form/a')
# element.click()
# except:
# pass
#
#用戶未認證狀態下會出現認證彈窗,如果不需就注釋掉
# """關閉認證彈窗"""
# try:
# element = wd.find_element_by_xpath('/html/body/div[5]/div/table/tbody/tr[1]/td/button')
# element.click()
# except:
# pass
"""跳轉 在線學習"""
element
=
wd.find_element_by_xpath(
'/html/body/section/div[2]/div[2]/div[1]/div[2]/div[3]/div[1]/img'
)
element.click()
print
(
"在線學習"
)
time.sleep(
5
)
''' 以下代碼用於獲取題庫及答案
"""跳轉 課程-練習 獲得所有題庫"""
for b in range(3,8):
course_xpath = '//*[@id="columnList"]/div[' + str(b) + ']/div/div[2]/div[1]'
practice_xpath = '//*[@id="columnList"]/div[' + str(b) + ']/div/div[2]/div[3]/div[2]'
element = wd.find_element_by_xpath(course_xpath)
course = element.text
print(f"打開課程 {course}")
practice = wd.find_element_by_xpath(practice_xpath)
practice.click()
"""獲得該課程題庫"""
for num in range(1,31): #練習題目是30題,因此依次答題
question = wd.find_element_by_xpath('//*[@id="exam_question"]').text #獲得題目
print(question) #打印
for i in range(1,5): # 4個選項
choose_xpath = '//*[@id="exam_answer"]/div[' + str(i) + ']/span[2]' #選項的xpath
choose = wd.find_element_by_xpath(choose_xpath) #選擇第i個
choose.click() #點擊選項
time.sleep(3) #延時3秒
all_answer_xpath = '//*[@id="exam_answer"]/div[' + str(i) + ']' #選項選擇后的xpath
all_answer = wd.find_element_by_xpath(all_answer_xpath)
answer_class_name = all_answer.get_attribute('class') #獲得class
print(answer_class_name) #查看
if answer_class_name == "item error" or answer_class_name == "item error": #如果是錯誤就
pass #跳過
#不知道為什么直接定位by_class_name(“item success”)無法找到,只好有這樣的辦法來通過判斷class的值來確定答案
#用by_class_name(“item success”)直接定位效率更高
elif answer_class_name == "item success" or answer_class_name =="item success": #如果是正確
answers = ['A','B','C','D'] # A,B,C,D
answer = answers[i-1] #在列表中的順序
answer_text = choose.text
print(answer) #打印
print(answer_text)
try: #嘗試
#當class=="item error"時沒有answer,故需要用try
with open("憲法題庫.txt","a") as f:
f.write(question + "/" + answer +"/" + answer_text + "/") #寫入題目和答案
except:
pass #跳過
if num < 30:
next = wd.find_element_by_id('next_question') #下一題
next.click() #點擊
time.sleep(0.1)
else:
pass
back_learn_list = wd.find_element_by_xpath('/html/body/section/div/div[2]/div[2]/span') #返回主題列表xpath
back_learn_list.click() #點擊
time.sleep(3) #延遲3秒
'''
"""導入題庫"""
file
=
"憲法題庫_全.txt"
#題庫文件路徑
questions
=
open
(
file
,
"r"
).read()
#文本格式打開
print
(questions)
question_list
=
questions.split(
'/'
)
#用split()函數根據 / 進行切片,生成題庫列表
"""跳轉 綜合測評"""
element
=
wd.find_element_by_xpath(
'//*[@id="toEvaluation"]'
)
element.click()
print
(
"進入綜合測評,即將開始答題……"
)
"""跳轉 開始答題頁面"""
element
=
wd.find_element_by_xpath(
'/html/body/section/div/div[2]/div/div[2]/div[3]/span'
)
#開始答題
print
(element.text)
element.click()
#點擊
for
i
in
range
(
1
,
11
):
#一共10道題目,循環10次
element
=
wd.find_element_by_xpath(
'//*[@id="exam_question"]'
)
#找到題目信息的xpath
print
(element.text)
#打印查看
question
=
element.text
#吧問題文本賦值給question
question
=
question.strip()
#刪除兩端空字符
if
question
in
question_list:
#如果題目再題庫列表中
print
(question_list.index(question))
#打印所在索引編號
answer
=
question_list[question_list.index(question)
+
1
]
#答案的索引編號是問題索引編號+1
print
(answer)
#打印答案
if
answer
=
=
"A"
:
#如果是A
answer_xpath
=
'//*[@id="exam_answer"]/div[1]/span[2]'
#A項的xapth
elif
answer
=
=
"B"
:
#如果是B
answer_xpath
=
'//*[@id="exam_answer"]/div[2]/span[2]'
#B項的xpath
elif
answer
=
=
"C"
:
answer_xpath
=
'//*[@id="exam_answer"]/div[3]/span[2]'
elif
answer
=
=
"D"
:
answer_xpath
=
'//*[@id="exam_answer"]/div[4]/span[2]'
time.sleep(
3
)
#延時3秒,模擬人工操作選擇
answer_element
=
wd.find_element_by_xpath(answer_xpath)
#找到證選項的xpath
answer_element.click()
#點擊
next_xpath
=
'/html/body/section/div/div[2]/div/div[2]/div[4]/div[2]/div[2]'
#下一題的xpath
next_element
=
wd.find_element_by_xpath(next_xpath)
#找到下一題
next_element.click()
#點擊
submit_element
=
wd.find_element_by_id(
"submit"
)
#提交按鈕
submit_element.click()
#點擊提交
"""關閉瀏覽器交卷成功彈窗"""
try
:
a
=
wd.switch_to.alert
# 切換alert
print
(a.text)
# 獲取彈窗上的文本
a.accept()
# 確認,相當於點擊[確定]按鈕
# a.dismiss() # 取消,相當於點擊[取消]按鈕
except
:
pass
|
鏈接:https://pan.baidu.com/s/1tKPO0oK0z2bb7ldIos6-Hw
提取碼:bltb