腾讯问卷自动填写抢名额
腾讯问卷自动填写从而实现抢名额
Python + Selenium
效果演示
前置
1.Python
2.Selenium
3.ChromeDriver
安装 Python
直接通过官网安装。
在安装过程中,确保选中了“Add Python to PATH”选项。
安装 Selenium
在终端中执行命令安装。
pip install selenium
ChromeDriver
下载网址
下载 Stable 版本。
实现
填空
使用对填空的题目中关键词进行定位的 XPath 。
以某个填空题为例,看一下它 HTML 的组成:
<section class="question question-type-text" id="question_q-4-a5hY" data-question-id="q-4-a5hY">
<div class="question-head" id="question_q-4-a5hY-head">
<div class="question-tags"></div>
<h2 class="question-title" aria-label="03, 必填, 你们队的队名是:, 单行文本"><span class="question-seq" aria-hidden="true"><b>03</b></span><span class="question-required" aria-hidden="true"><i aria-hidden="true">*</i><i style="position: absolute; width: 1px; height: 1px; padding: 0px; margin: -1px; overflow: hidden; clip: rect(0px, 0px, 0px, 0px); white-space: nowrap; border: 0px;">必填</i></span><div class="text" aria-hidden="true"><div class="pe pe-view"><p class="pe-line">你们队的队名是:</p></div></div></h2>
</div>
<div class="question-body question-cont"><input class="inputs-input" type="text" placeholder="请输入" value="">
</div>
</section>
可以发现,问题和要填的空是定义在两个并列的div中的。
需要指出的是,网上有大量“腾讯问卷/问卷星自动填写教程”,但他们都是通过定位到“问题”所在的div并直接点击,显然对于新版的问卷星/腾讯问卷是不起作用的。
所以关键在于如何写好 XPath。我们可以先通过关键词定位到第一个div,再通过第一个div顺势定位到在它下面的div。
于是我们可以通过以下XPath代码实现定位到该文本框并进行填写:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
k = WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((By.XPATH, "//section[contains(@class, 'question')][.//p[contains(text(), '队名')]]//input[@type='text']"))
) #定位
k.send_keys("嘤嘤嘤") #填写
选择
直接通过按钮的文本对按钮进行定位。例:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
k = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//label[contains(., '男'))]"))
)
k.click()
示例代码
import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
option = webdriver.ChromeOptions()
# 下面两句反爬虫用
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
#这一句将自己的 ChromeDriver 路径复制过去
service = Service(executable_path="D:/chromedriver-win64/chromedriver.exe")
driver = webdriver.Chrome(service=service, options=option)
# 下面一句同样反爬虫用
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
# 定时打开网页填写
tt = datetime.datetime(2025,6,4,20,0,1)
while True:
ct = datetime.datetime.now()
if ct >= tt:
break
time.sleep(1)
# 打开网页
driver.get("https://wj.qq.com/s2/22607300/0395/")
# 选择
k = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//label[contains(., '男')]"))
)
k.click()
# 填空
try:# 使用 try except 实现问卷中找不到当前问题仍然可以继续往后填写
k = WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((By.XPATH, "//section[contains(@class, 'question')][.//p[contains(text(), '队名')]]//input[@type='text']"))
)
k.send_keys("嘤嘤嘤")
except Exception as e:
pass
# “已阅读并同意”对钩
k = WebDriverWait(driver, 1).until(
EC.element_to_be_clickable((By.XPATH, "//label[contains(@class,'t-checkbox')]"))
)
k.click()
# 提交按钮
k = WebDriverWait(driver, 1).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'btn-submit')]"))
)
k.click()

浙公网安备 33010602011771号