學(xué)習(xí)python有一段時(shí)間了录语,一直沒有一個(gè)完整的項(xiàng)目魂迄。這次總算完成了一個(gè)小的項(xiàng)目-自動打開網(wǎng)頁答題,雖然還不是很完美耸三,也足夠用了乱陡。
分享下過程。
首先理清思路:解析網(wǎng)頁-->提取題目-->判斷是否存在題庫中-->答題
1吕晌、解析網(wǎng)頁蛋褥,我想到了爬蟲中使用的request模塊,但是考慮到后邊的需要點(diǎn)擊進(jìn)行答題睛驳,最后在網(wǎng)上找到了更好用的selenium模塊烙心。然后進(jìn)行selenium的相關(guān)配置(配置過程有許多坑,最為關(guān)鍵的就是瀏覽器對應(yīng)的版本和webdriver存放位置)乏沸,學(xué)習(xí)selenium的相關(guān)用法淫茵,為了更好的定位網(wǎng)頁中的對象安裝了selenium IDE插件。經(jīng)過半天努力蹬跃,終于能通過Firefox打開網(wǎng)頁了匙瘪。
2、通過selenium IDE插件確定要提取對象的位置蝶缀,這個(gè)可以直接在瀏覽器的擴(kuò)展程序中直接查找添加丹喻,添加好了記得重啟瀏覽器。在定位對象過程中使用了id翁都,class碍论,css和xpath方法。搜索到以后使用get_attribute得到要操作頁面的地址柄慰。
3鳍悠、使用文本處理split方法對地址進(jìn)行解析税娜,提出題目中的題號,然后在已經(jīng)知道答案的題庫中搜索藏研,如果有敬矩,返回答案,如果沒有全部選擇a蠢挡。(由于不限制挑戰(zhàn)次數(shù)弧岳,但是每天最多挑戰(zhàn)成功3次。所以只答最高分值的題袒哥,其他都打錯(cuò))
4缩筛、根據(jù)答案選擇按鈕,然后點(diǎn)擊下一個(gè)堡称,最后提交。然后再進(jìn)行下一題艺演,直到挑戰(zhàn)成功三次却紧。
在答題過程中還出現(xiàn)了限時(shí)答題的情況,需要判斷是否存在和跳過胎撤,又進(jìn)行進(jìn)一步完善晓殊。
通過這次完整的小項(xiàng)目,有點(diǎn)小總結(jié)伤提,就是網(wǎng)上有許多前人走過的路巫俺,總結(jié)好的經(jīng)驗(yàn),學(xué)習(xí)就是了肿男,不過要懂得區(qū)分好與壞介汹。先找到最核心的問題,簡化為一個(gè)小的問題舶沛,然后在網(wǎng)上查找最簡單精準(zhǔn)的答案嘹承。先解決了,然后再確定一個(gè)問題進(jìn)行解決如庭,就這樣逐步進(jìn)行完善叹卷。
腳本如下:
'''
作者:Yeahlv
時(shí)間:2019-4-13
版本:v1.0
開發(fā)環(huán)境:Pycharm
'''
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import numpy as np
import time
題目的序號
timu_id_ar = [7,12,13,14,16,39,40]
題目的答案
timu_daan_ar=np.array([[3,4,4,4,4,3,4,4,4,1],[4,1,2,2,3,2,4,3,2,3],[2,1,2,3,4,4,4,3,2,2],[4,1,2,3,2,4,4,3,4,2],[1,2,2,3,4,3,2,1,2,3],[1,4,3,2,2,2,2,4,2,2],[4,1,2,2,2,2,3,4,2,2],[1,1,1,1,1,1,1,1,1,1]])
def wait_response_time(chrome,waittime,func):
# 返回 func執(zhí)行結(jié)果
return WebDriverWait(chrome,waittime).until(func)
def automatic_login(name,pwd,url):
#chrome = webdriver.Firefox()
chrome.get(url)
time.sleep(2)
name_label = chrome.find_element_by_name("LoginForm[loginName]")
name_label.send_keys(name)
pwd_label = chrome.find_element_by_name("LoginForm[password]")
pwd_label.send_keys(pwd)
time.sleep(1)
login_label = chrome.find_element_by_class_name('login-submit')
login_label.click()
time.sleep(2)
chrome.get('https://www.qqyuedu.com/student-home/challenge/index')
獲取題目地址
def huoqu_timu(url):
chrome.get(url)
is_ks = isElementExist_lt("開始挑戰(zhàn)")
if is_ks:
challenge_label = chrome.find_element_by_link_text("開始挑戰(zhàn)")
dizhi=challenge_label.get_attribute("href")
print('挑戰(zhàn)題目的地址是:',dizhi)
return dizhi
else:
return False
def isElementExist_lt(link_text):
try:
chrome.find_element_by_link_text(link_text)
return True
except:
return False
def isElementExist_class(class_name):
try:
chrome.find_element_by_class_name(class_name)
return True
except:
return False
def isElementExist_path(path_name):
try:
chrome.find_element_by_xpath(path_name)
return True
except:
return False
def daan_trsf(num):
if num==1:
return "A"
if num==2:
return "B"
if num==3:
return "C"
if num==4:
return "D"
if num==5:
return "E"
else:
return "答案是不是輸入錯(cuò)誤?"
答題
def tiaozhan(url):
chrome.get(url)
# 取得題目索引
def timu_index(url):
num1 = int(url.split("=")[1].split("&")[0])
if num1 in timu_id_ar:
timu_index = timu_id_ar.index(num1)
else:
timu_index = 7
print("這道題不在題庫內(nèi)坪它,答案全部選擇A骤竹。")
return timu_index
#開始挑戰(zhàn)
is_ks = isElementExist_lt("開始")
if is_ks:
challenge_label = chrome.find_element_by_link_text("開始")
challenge_label.click()
time.sleep(1)
#開始挑戰(zhàn)
is_ks = isElementExist_lt("開始挑戰(zhàn)")
if is_ks:
challenge_label = chrome.find_element_by_link_text("開始挑戰(zhàn)")
challenge_label.click()
time.sleep(1)
x = timu_index(url)
print("題目序號是"+str(x), "題目答案是",timu_daan_ar[x])
for i in range(10):
is_ks = isElementExist_lt("開始")
if is_ks:
challenge_label = chrome.find_element_by_link_text("開始")
challenge_label.click()
time.sleep(1)
# 點(diǎn)跳過
is_tg = isElementExist_lt("跳過")
if is_tg:
challenge_label = chrome.find_element_by_link_text("跳過")
challenge_label.click()
time.sleep(1)
# 點(diǎn)擊答案,模式css=.question-option-list-item:nth-child(1) input
#x=timu_index(url)
timu_daan_txt = ".question-option-list-item:nth-child("+str(timu_daan_ar[x][i])+") input"
print("第"+str(i+1)+"題選擇:",daan_trsf(timu_daan_ar[x][i]))
challenge_label = chrome.find_element_by_css_selector(timu_daan_txt)
challenge_label.click()
time.sleep(1)
# 點(diǎn)擊提交答案 xpath=//button[contains(.,'提交答案')]
is_tjda = isElementExist_path("http://button[contains(.,'提交答案')]")
if is_tjda:
challenge_label = chrome.find_element_by_xpath("http://button[contains(.,'提交答案')]")
challenge_label.click()
time.sleep(1)
break
is_xyt = isElementExist_class("ion-chevron-right")
if is_xyt:
challenge_label = chrome.find_element_by_class_name("ion-chevron-right")
challenge_label.click()
time.sleep(1)
is_tjda = isElementExist_class("btn btn-primary btn-lg")
if name == "main":
chrome = webdriver.Firefox()
name = "lvjianyi20090601"
pwd = "123456"
url = "https://www.qqyuedu.com/"
automatic_login(name,pwd,url)
#https://www.qqyuedu.com/challenge/default/quiz?challengeId=9&quizId=68&layoutMode=iframe
lyt = 0
while 1>0:
url_1=huoqu_timu('https://www.qqyuedu.com/student-home/challenge/index')
if url==False:
break
else:
tiaozhan(url_1)
lyt += 1
print("第"+str(lyt)+"次挑戰(zhàn)")