Python骚操作来了~
用Python来实现科目一/四自动答题,100分不要太简单!
最初是表弟最近想买车,但是驾照都没有,买什么车,只能先考驾照~
看他在网页上练习题目慢吞吞的,我就看不下去了,直接给他来一手揠苗助长~
当时就用Python整了几十行代码,给他实现一下自动答题,我说你什么时候答题这么快,我就给你买个车!
嘿嘿,悄悄告诉你们,那是不可能的,不信你们看案例演示!
就这速度,我可没加速哈~
代码仅做展示
网址我屏蔽了,不然系统不给过~
模块
import requests import parsel from selenium import webdriver
发送请求
def get_response(html_url): """ :param html_url: 请求网址 :return: 响应对象 """ headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response
获取答案链接
def get_answer_url(html_url): """ :param html_url: :return: """ html_data = get_response(html_url).text selector = parsel.Selector(html_data) answer_url_id = selector.css('.Content li::attr(c)').getall() answer_url_list = [f'https://地址我屏蔽了,不然不给过/Post/{i}.htm' for i in answer_url_id] return answer_url_list
获取答案
def get_answer_result(answer_url): """ :param answer_url: :return: """ answer_data = get_response(html_url=answer_url).text selector = parsel.Selector(answer_data) question = selector.css('#question a::text').get() answer = selector.css('#question h1 u::text').get() if answer == '对': answer = '正确' elif answer == '错': answer = '错误' answer_dict = { '问题': question, '答案': answer } return answer_dict
获取所有问题以及答案
def get_all_answer(answer_url_list): """ 完整源码都放在这个裙了 279199867 :param link: :return: """ page = 1 for answer_url, li in zip(answer_url_list, lis): answer_dict = get_answer_result(answer_url) bs = li.find_elements_by_css_selector('b') num = 1 for b in bs: choose = b.text if len(choose) > 2: choose = choose[0] print(choose, answer_dict['答案']) if choose in answer_dict['答案']: driver.find_element_by_css_selector(f'#LI{page} b:nth-child({num+2})').click() print('点击了') else: print('没有点击') num += 1 page += 1 print(answer_dict)
完整源代码下方名片获取哈~
最后给大家推荐一套Python视频:代码总是学完就忘记?100个实战项目!让你沉迷学习丨学以致用丨下一个Python大神就是你!