考试宝导出文件

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
# chrome_proxy.exe

def replace_letter(text, replace_letter):
    pattern = re.compile(r'\b{}\b'.format(replace_letter))
    return pattern.sub(replace_letter, text)

# 先切换到chrome可执行文件的路径
os.chdir(r"C:\\Program Files (x86)\\Google\\Chrome\\Application")
# # user-data-dir为路径
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)  # 自带文件关闭功能,不需要再写f.close()
    # 第1条数据 最大化窗口
    if i == 0:
        driver.maximize_window()
        time.sleep(1)
    # 点击下一条
    # driver.find_element(By.CLASS_NAME, 'el-button el-button--primary el-button--small').click()
    driver.find_element_by_xpath('//button[@class="el-button el-button--primary el-button--small"]').click()
    time.sleep(3)



# 退出浏览器
driver.quit()

你可能感兴趣的:(python)