Python实操实现驾考自动答题

from selenium import webdriver
from selenium.webdriver.common.by import By
import requests
import parsel
#


def get_all_answer(answer_url_list):
    for answer_url in answer_url_list:
        html_data = requests.get(url=answer_url).text
        selector = parsel.Selector(html_data)
        question = selector.css('#WinContent div strong a::text').get()
        answer = selector.css('#WinContent div ul li i u::text').get()
        if answer == '对':
            answer = '正确'
        elif answer == '错':
            answer = '错误'
        dit = {
            '问题': question,
            '答案': answer
        }
        return dit


def AnswerQuestions():
    pass


if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('https://www.ybjk.com/ks/mhkek.htm')
    # Li标签
    lis = driver.find_elements(By.CSS_SELECTOR, '#ExamList li')
    # 列表推导式
    answer_url = [
        f'https://www.ybjk.com/tiku/{li.get_attribute("c")}.htm' for li in lis]
    # for li in lis:
    #     answer = li.get_attribute('c')
    #     print(answer)
    answer_list = get_all_answer(answer_url)
    page = 1
    for li, url in zip(lis, answer_url):
        bs = li.find_element(By.CSS_SELECTOR, 'b')
        answer_dit = get_all_answer(url)
        num = 1
        for b in bs:
            choose = b.text
            if len(choose) > 2:
                choose = choose[:1]

            if choose in answer_dit['答案']:
                driver.find_element(
                    By.CSS_SELECTOR, f'#LI{page} b:nth-child({num+2})').click()
            num += 1
        page += 1

你可能感兴趣的:(Python,python,爬山算法)