自动答题脚本,自动答题驾考试题

from selenium import webdriver
import requests
import parsel
def get_all_answer(answer_url):
    html_data = requests.get(url=answer_url).text
    # 解析数据
    selector = parsel.Selector(html_data)        # 获取问题
    question = selector.css('#question h1 strong a::text').get()
    answer = selector.css('#question h1 u::text').get()
    if answer == '对':
        answer = '正确'
    elif answer == '错':
        answer = '错误'
    dit = {
            '问题': question,
            '答案': answer
        }
    return dit


if __name__ == '__main__':
    path = 'chromedriver.exe'
    browser = webdriver.Chrome(path)
    url = 'https://kmy.jsyks.com/hn'
    browser.get(url)
    # 全屏
    browser.maximize_window()
    lis = browser.find_elements_by_css_selector('.Content li')  # 获取class=Content的ul的下面所有的li标签
    answer_url_list = [f'https://tiba.jsyks.com/Post/{li.get_attribute("c")}.htm' for li in lis] # 获取li标签中属性名为c的对应的属性值,生成答案链接

    page = 1
    for li,answer_url in zip(lis,answer_url_list):
        answer_dit = get_all_answer(answer_url)
        bs = li.find_elements_by_css_selector('b') # 在li标签中找b标签
        # 获取b标签中的值
        num = 1
        for b in bs:
            choose = b.text
            print(choose)
            if len(choose) > 2:
                choose = choose[0]
            if choose in answer_dit['答案']:
                browser.find_element_by_css_selector(f'#LI{page} b:nth-child({num+2})').click()
            num += 1
        page += 1

你可能感兴趣的:(selenium,python)