import webbrowser
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
import subprocess
import re
def replace_letter(text, replace_letter):
pattern = re.compile(r'\b{}\b'.format(replace_letter))
return pattern.sub(replace_letter, text)
os.chdir(r"C:\\Program Files (x86)\\Google\\Chrome\\Application")
subprocess.Popen('chrome.exe --remote-debugging-port=9527 --user-data-dir="D:\project\kaoshibao\AutomationProfile"')
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9527")
driver = webdriver.Chrome(options=chrome_options)
driver.get(
'https://www.kaoshibao.com/login/?source=https%3A%2F%2Fwww.kaoshibao.com%2Fsctk%2F')
input('输入空格继续程序...')
driver.get(
'https://www.kaoshibao.com/online/?paperId=8528245&practice=&modal=1&is_recite=&qtype=&text=%E9%A1%BA%E5%BA%8F%E7%BB%83%E4%B9%A0&sequence=226&is_collect=1&is_vip_paper=0')
input('输入空格继续程序...')
driver.implicitly_wait(1)
for i in range(900):
i=i+226
time.sleep(1)
all = []
print("第"+str(i+1)+"次")
title = driver.find_element_by_xpath("//div[@class='qusetion-box']").text
try :
analysis = driver.find_element_by_xpath("//p[@class='answer-analysis']").text
except NoSuchElementException:
analysis='无'
answer = driver.find_element_by_xpath("//div[@class='right-ans']//span").text
part = driver.find_element_by_xpath("//div[@class='select-left pull-left options-w']").text.replace("\n", ".")
part = part.replace('.B.', '\nB.', 1)
part = part.replace('.C.', '\nC.', 1)
part = part.replace('.D.', '\nD.', 1)
part = part.replace('.E.', '\nE.', 1)
part = part.replace('.F.', '\nF.', 1)
title = str(i + 1) + "." + title
answer = "答案:" + answer
analysis = "解析:" + analysis
all.append(title)
all.append(part)
all.append(answer)
all.append(analysis)
ques = title + ' \n' + part + '\n' + answer + ' \n' + analysis + '\n '
print("ques"+ques)
with open(r"D:\\project\\kaoshibao\\2.txt", "a",encoding='utf-8') as f:
f.write(ques)
if i == 0:
driver.maximize_window()
time.sleep(1)
driver.find_element_by_xpath('//button[@class="el-button el-button--primary el-button--small"]').click()
time.sleep(3)
driver.quit()