0
点赞
收藏
分享

微信扫一扫

python爬虫selenium自动化-实现科目一自动答题

梦为马 2023-10-26 阅读 42

通过selenium模块

实现驾驶员考试网站的科目一的自动考试答题

公众号回复 自动答题 获取源代码


打开这个网站

python爬虫selenium自动化-实现科目一自动答题_html

然后打开开发者模式,找到这些一个一个的题,每个li标签对应一个题,每个li标签上都有一个c属性

python爬虫selenium自动化-实现科目一自动答题_公众号_02


然后打开这个网址,这个网址存放着该题的答案,这个url后面接的是刚才的c属性值,对应着该题的答案

python爬虫selenium自动化-实现科目一自动答题_公众号_03

写代码,导入selenium包,导入webdrive模块

打开网站

python爬虫selenium自动化-实现科目一自动答题_公众号_04

获取c属性,打开存放答案的地址获取答案

while a<=100:
    # 获取答案网页地址,拼接答案网页地址
    d = driver.find_element(By.XPATH,f'.//ul[@class="Content"]/li[{a}]').get_attribute('c')
    dz = f"https://tiba.jsyks.com/Post/{d}.htm"
    # 从网页找到答案
    r = requests.get(url=dz).text
    html = etree.HTML(r)
    daan = html.xpath("/html/body/div[2]/div[4]/div[1]/div[1]/h1/u/text()")
    da = ''.join(daan)


判断对错

# 判断然后点击
    if da == '对':
        driver.find_element(By.XPATH,f'//*[@id="LI{a}"]/b[1]').click()
    elif da == '错':
        driver.find_element(By.XPATH, f'//*[@id="LI{a}"]/b[2]').click()
    elif da == 'A':
        driver.find_element(By.XPATH, f'//*[@id="LI{a}"]/b[1]').click()
    elif da == 'B':
        driver.find_element(By.XPATH, f'//*[@id="LI{a}"]/b[2]').click()
    elif da == 'C':
        driver.find_element(By.XPATH, f'//*[@id="LI{a}"]/b[3]').click()
    else:
        driver.find_element(By.XPATH, f'//*[@id="LI{a}"]/b[4]').click()
    a += 1

点击交卷

# 点击交卷
driver.find_element(By.CLASS_NAME,'btnJJ').click()
time.sleep(5)
driver.quit()

看效果

python爬虫selenium自动化-实现科目一自动答题_html_05

公众号回复 自动答题 获取源代码

感谢观看


举报

相关推荐

0 条评论