1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| import time import re from playwright.sync_api import sync_playwright from bs4 import BeautifulSoup import os import requests import json from openai import OpenAI import openai
def doubaoAI(user_content): client = OpenAI( base_url="xxxx", api_key='xxx', )
response = client.chat.completions.create( model="doubao-seed-1-6-250615", messages=[ { "role": "user", "content": user_content } ], ) return response.choices[0].message.content
def pollAI(user_content): url = "https://text.pollinations.ai/openai" availiable_models = ["openai-large", 'openai', 'openai-fast', 'qwen-coder', "grok", 'searchgpt'] for model in availiable_models: try: payload = { "model": model, "messages": [ {"role": "user", "content": user_content} ], "seed": 101, "temperature":0.1, } headers = { "Content-Type": "application/json" }
response = requests.post(url, headers=headers, json=payload) response.raise_for_status() return response.json()['choices'][0]['message']['content'] except: continue
def ai_answer(user_content): res = pollAI(user_content) return res
def process_div(): html_content = page.content() soup = BeautifulSoup(html_content, "html.parser") div = soup.find("div", class_="quiz-content")
quiz = div.find('div', class_='quiz-question') quiz_text = quiz.text print(quiz_text)
answer_box = div.find('div', class_='quiz-right-box') labels = answer_box.find_all('label') quiz_ls = {}
input_type = "radio"
quiz_options = '' for i, quiz_opt in enumerate(labels): quiz_input = quiz_opt.find('input') if i == 0: input_type = quiz_input.get('type')
option_text = quiz_opt.find('p').text quiz_ls[i] = quiz_input.get('value') quiz_options += f'{i}:{option_text}\n\n'
user_content = f''' 你是Python专家,下面是一个python问题,帮我解答一下。
问题是: {quiz_text} 选项是: {quiz_options}
只输出答案的编号即可, 如果有多个答案就以英文逗号分隔, 不要输出多余的内容。 ''' res = ai_answer(user_content) res = res.split(',') for r in res: input_value = quiz_ls.get(int(r), quiz_ls[0]) selector = f'input[type="{input_type}"][value="{input_value}"]' page.click(selector) time.sleep(1)
def fake_submit(target_url): page.goto(target_url) page.wait_for_load_state("networkidle")
button_start = page.locator('button', has_text=re.compile("Start|Proceed", re.IGNORECASE)) button_start.click() page.wait_for_load_state("networkidle") time.sleep(0.5) button_accept = page.locator('button').filter(has_text='I accept') if button_accept.count() == 1: button_accept.click() page.wait_for_load_state("networkidle") time.sleep(0.5) button_start = page.locator('button', has_text=re.compile("Start|Proceed", re.IGNORECASE)) button_start.click()
page.wait_for_selector('div.quiz-content')
while True: time.sleep(3) process_div()
button_next = page.locator('button:has-text("Next")') try: if button_next.is_visible(): button_next.click() page.wait_for_selector('div.quiz-content') time.sleep(1) else: break except TimeoutError: break
page.on("dialog", lambda dialog: dialog.accept())
submit_button = page.locator('button:has-text("Submit")') submit_button.click()
page.wait_for_selector('button:has-text("Submit")', timeout=5000) confirm_submit = page.locator('button:has-text("Submit")') confirm_submit.click()
page.wait_for_selector('#submit-quiz-modal', timeout=5000)
page.locator('#submit-quiz-modal button:has-text("Yes")').click() time.sleep(5)
with sync_playwright() as p: browser = p.chromium.launch(headless=True) context = browser.new_context() page = context.new_page()
login_url = "https://edube.org/login" page.goto(login_url)
page.fill('input[name="_username"]', 'xxx') page.fill('input[name="_password"]', 'xxx')
page.click('button[type="submit"]')
page.wait_for_load_state('networkidle')
quiz_urls = [ "https://edube.org/quiz/pe-2/pe2-module-1-quiz-1", "https://edube.org/quiz/pe-2/pe2-module-1-test-1", "https://edube.org/quiz/pe-2/pe2-module-2-quiz-1", "https://edube.org/quiz/pe-2/pe2-module-2-test-1", "https://edube.org/quiz/pe-2/pe2-module-3-quiz-1", "https://edube.org/quiz/pe-2/pe2-module-3-test-1", "https://edube.org/quiz/pe-2/pe2-module-4-quiz-1", "https://edube.org/quiz/pe-2/pe2-module-4-test-1", 'https://edube.org/quiz/pe-2/pe2-summary-test-1' ] for url in quiz_urls: fake_submit(url) time.sleep(3)
browser.close()
|