考试宝导出文件

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()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值