最近有一个社会实践需要做问卷调查,突发奇想搞一个自动化脚本,省事省米

1 下载依赖selenium

selenium是一款网页爬虫重要的工具。

2  安装chrome驱动

这里需要准备chrome浏览器以及对应的驱动。需要注意的是驱动的版本需要和chrome保持一致。
chrome浏览器版自行查看本机安装版本。

python实现问卷星自动填写(可以跳过智能验证)

chrome驱动下载链接 chrome驱动链接找到对应版本的驱动。 

 python实现问卷星自动填写(可以跳过智能验证)

下载后将安装包解压(解压后为.exe文件)在这个文件夹下 

python实现问卷星自动填写(可以跳过智能验证) 

  3 导入相关的包

from selenium import webdriver
import time
import random
from selenium.webdriver.common.by import By
import pyautogui

防止被识别为脚本

option = webdriver.ChromeOptions()
        option.add_experimental_option('excludeSwitches', ['enable-automation'])
        option.add_experimental_option('useAutomationExtension', False)
        browser = webdriver.Chrome(options=option)
        browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                                {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
        browser.maximize_window()  # 窗口最大化
        # 这行代码的作用是将webdriver这个属性置为undefined
        browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                            {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
                             })
        browser.get(url)
        time.sleep(2)

  

代码如下

# -*- coding: utf-8 -*-
# @Time : 2023/1/26 11:34 
# @Author : wkk
# @File : Python自动化填问卷星.py
from selenium import webdriver
import time
import random
from selenium.webdriver.common.by import By
import pyautogui
def run(num, url):
    for i in range(num):
        option = webdriver.ChromeOptions()
        option.add_experimental_option('excludeSwitches', ['enable-automation'])
        option.add_experimental_option('useAutomationExtension', False)
        browser = webdriver.Chrome(options=option)
        browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                                {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
        browser.maximize_window()  # 窗口最大化
        # 这行代码的作用是将webdriver这个属性置为undefined
        browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                            {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
                             })
        browser.get(url)
        time.sleep(2)
        num1 = random.randint(1, 2)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q1_{num1}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q1_{num1}']").click()
        time.sleep(0.5)
        num2 = random.randint(1, 3)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q2_{num2}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q2_{num2}']").click()
        time.sleep(0.5)
        num3 = random.randint(1, 3)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q3_{num3}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q3_{num3}']").click()
        time.sleep(0.5)
        num4 = random.randint(1, 3)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q4_{num4}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q4_{num4}']").click()
        time.sleep(0.5)
        num5 = random.randint(1, 2)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q5_{num5}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q5_{num5}']").click()
        time.sleep(0.5)
        num6 = random.randint(1, 3)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q6_{num6}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q6_{num6}']").click()
        time.sleep(0.5)
        num7 = random.randint(1, 4)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q7_{num7}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q7_{num7}']").click()
        time.sleep(0.5)
        # num8 = random.randint(1, 3)
        # if browser.find_element(by=By.XPATH, value=f"//div[@for='q8_{num8}']"):
        #     browser.find_element(by=By.XPATH, value=f"//div[@for='q8_{num8}']").click()
        # time.sleep(0.5)
        # 问题5
        randomId = random.randint(1, 3)  # 随机数选择(选多少个)
        for i in range(1, randomId + 1):  # 循环 实现多选效果
            randomId1 = random.randint(1, 3)  # 随机选择第1到第6个选项之一
            # 两种js实现方式
            js = "document.getElementById(\"q8_" + str(randomId1) + "\").checked = true"
            browser.execute_script(js)
        # 延时
        time.sleep(1)
        num9 = random.randint(1, 7)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q9_{num9}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q9_{num9}']").click()
        time.sleep(0.5)
        num10 = random.randint(1, 3)
        if browser.find_element(by=By.XPATH, value=f"//div[@for='q10_{num10}']"):
            browser.find_element(by=By.XPATH, value=f"//div[@for='q10_{num10}']").click()
        time.sleep(0.5)
        if browser.find_element(by=By.XPATH, value="//div[@class='submitbtn mainBgColor']"):
            browser.find_element(by=By.XPATH, value="//div[@class='submitbtn mainBgColor']").click()
        time.sleep(1)
        try:
            element = browser.find_element(By.CSS_SELECTOR, "#alert_box > div:nth-child(2) > div:nth-child(2) > button")
            element.click()
            time.sleep(1)
            yanz = browser.find_element(By.ID, "rectMask")
            yanz.click()
            time.sleep(4)
        except:
            pass
        # 先点确认
        try:
            browser.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
            time.sleep(1)
        except:
            pass
        # 再点智能验证提示框,进行智能验证
        try:
            browser.find_element(By.XPATH, '//*[@id="SM_BTN_WRAPPER_1"]').click()
            time.sleep(3)
        except:
            pass
        # 滑块验证
        pyautogui.moveTo(789, 805, duration=1)  # 传入屏幕的位置
        time.sleep(0.5)
        pyautogui.dragRel(370, 0, duration=0.8)  # 拖动
        # pyautogui.moveTo(789, 805, duration=1)   # 回到原处
        # pyautogui.moveRel(370, 0, duration=1)
        # pyautogui.dragRel(256, 0, duration=0.8)
        # pyautogui.moveTo(789, 805)
        # time.sleep(0.5)
        # pyautogui.dragRel(370, 0, duration=0.8)
        time.sleep(5)
        browser.close()
        print(f"第{i + 1}份已经完成")
if __name__ == '__main__':
    num = 10  # 填的份数
    url = 'https://www.wjx.cn/vm/tXQkrbD.aspx'  # 链接
    run(num, url)

以上代码是我根据我自己的问卷形式来的,具体参数还得跟着问卷走,代码亲测有效,当短时间内刷的问卷数过多时,可能会出现第二重智能验证,这就要手动验证了哦,所以为了防止出现二重智能验证,在每提交完一份问卷后,要用time.sleep函数用于延时 , 避免提交过快。

本文仅为技术交流,最好不要用来恶意填写别人的问卷,以造成问卷质量低下的情况,请大家还是根据自己的实际情况填写问卷哦。