Python騷操作來了~ 用Python來實現科目一/四自動答題,100分不要太簡單! 最初是表弟最近想買車,但是駕照都沒有,買什麼車,只能先考駕照~ 看他在網頁上練習題目慢吞吞的,我就看不下去了,直接給他來一手揠苗助長~ 當時就用Python整了幾十行代碼,給他實現一下自動答題,我說你什麼時候答題 ...
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大神就是你!