因为需要收集数据,但是要求又不太严格,找朋友填又比较麻烦所以做了一个工具来帮忙!!!
首先是要引入不同的库来保障代码正常运行
import time
import random
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
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.common.exceptions import TimeoutException, NoSuchElementException
然后是输入自己的问卷星网址
url = '你的问卷网址'
最重要的一步就是绕过问卷星的智能检测,否则后台不会有数据更新
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
我是用Chrome WebDriver 做的,用Edge的可以更换一下
web = Chrome(options=options)
关键的一步又来了,隐藏机器填写的痕迹,保障问卷星后台的数据更新
web.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
设置问卷份数
def main():
for i in range(40):
fill_out_form(web)
print(f"第{i + 1}问卷提交成功")
编写请求问卷页面、等待页面完全加载的代码
web.get(url)
wait = WebDriverWait(web, 10)
设置问题的选项,因为第一个问题一般是性别问题,所以我们设置为随机选择,等待两秒回答第二题,后面的都等待一段时间,防止封锁IP
qa_tmp = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='div1']")))
answers = qa_tmp.find_elements(By.XPATH, ".//div[@class='ui-radio']")
if answers:
answer = random.choice(answers)
answer.click()
time.sleep(2)
这一个和上面重复是设置第二个问题的
qa_tmp = wait.until(EC.visibility_of_element_located((By.XPATH, f"//*[@id='div2']")))
answers = qa_tmp.find_elements(By.XPATH, ".//div[@class='ui-radio']")
if answers:
answer = random.choice(answers)
answer.click()
time.sleep(2) # 等待2秒
这段代码是循环回答量表题(第3题到17题),并且设置答案在中立、同意、非常同意之间
for question_id in range(3, 18):
random_integer = random.randint(3, 5)
qa_tmp = wait.until(EC.visibility_of_element_located((By.XPATH, f"//*[@id='div{question_id}']")))
answers = qa_tmp.find_elements(By.XPATH, f"//*[@id='div{question_id}']/div[2]/div/ul/li[{random_integer}]")
if answers:
answer = random.choice(answers)
answer.click()
time.sleep(1.5)
后面就是答完题之后的提交按钮了,一定要等待一段时间确保提交成功执行下一回合
submit_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SubmitBtnGroup"]')))
submit_button.click()
# 一定要等待一段时间,确保提交成功
time.sleep(3)
最后就是提交成功显示,和不成功的显示
print(f"问卷提交成功")
except (TimeoutException, NoSuchElementException) as e:
print(f"问卷提交失败: {e}")
执行成功就可以填问卷了,全部代码如下:
import time
import random
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
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.common.exceptions import TimeoutException, NoSuchElementException
# 问卷网址
url = 'https://www.wjx.cn/vm/mIIHW56.aspx'
# 设置 Chrome 选项以绕过问卷星的智能检测
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
# 创建 Chrome WebDriver 实例
web = Chrome(options=options)
# 执行 CDP 命令,进一步隐藏自动化痕迹
web.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
def fill_out_form(web):
try:
# 请求问卷页面
web.get(url)
# 等待页面加载完全
wait = WebDriverWait(web, 10)
# 设置第一个问题的随机选择
qa_tmp = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='div1']")))
answers = qa_tmp.find_elements(By.XPATH, ".//div[@class='ui-radio']")
if answers:
answer = random.choice(answers)
answer.click()
time.sleep(2) # 等待2秒
# 设置第二个问题的随机选择 //*[@id="div2"]/div[2]/div[4]/span //*[@id="div2"]/div[2]/div[6]/span/a
qa_tmp = wait.until(EC.visibility_of_element_located((By.XPATH, f"//*[@id='div2']")))
answers = qa_tmp.find_elements(By.XPATH, ".//div[@class='ui-radio']")
if answers:
answer = random.choice(answers)
answer.click()
time.sleep(2) # 等待2秒
# 设置后续量表题的随机选择(中立、同意、非常同意)
for question_id in range(3, 23): # 假设有13个量表题,从第3题到第17题
random_integer = random.randint(4, 5)
qa_tmp = wait.until(EC.visibility_of_element_located((By.XPATH, f"//*[@id='div{question_id}']")))
answers = qa_tmp.find_elements(By.XPATH, f"//*[@id='div{question_id}']/div[2]/div/ul/li[{random_integer}]")
if answers:
answer = random.choice(answers)
answer.click()
time.sleep(1.5) # 等待2秒
# 点击提交按钮
submit_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SubmitBtnGroup"]')))
submit_button.click()
# 等待一段时间,确保提交成功
time.sleep(3)
print(f"问卷提交成功")
except (TimeoutException, NoSuchElementException) as e:
print(f"问卷提交失败: {e}")
def main():
for i in range(40): # 提交10份不同的问卷
fill_out_form(web)
print(f"第{i + 1}问卷提交成功")
if __name__ == "__main__":
main()
web.quit()