利用简单权重设置选项比例分配以及条件判断语句即可将问卷往你选择的放向走,需要对前端稍微了解即可,用谷歌的开发者工具查看元素
例如:check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")
from selenium import webdriver
import random
import time
import numpy as np
#pip install selenium==4.8.0 最新版本模块
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChainsdef get_ele_time(driver,time,func):WebDriverWait(driver,time).until(func)
def auto_write():# 防止被浏览器识别为脚本# 需要谷歌版本 109.0.5414.120(正式版本) (64 位),否则可能会报错chrome_options = Options()chrome_options.add_argument("--disable-blink-features=AutomationControlled")chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])chrome_options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36')driver = Chrome(options=chrome_options)#这里需要你写下你的问卷星的网址进行调研虫,例如我们的问卷driver.get('https://www.wjx.cn/vm/OpWPWY1.aspx#')# print('=============================================================')# 获取到所有题目的选项rank = driver.find_elements(by=By.CLASS_NAME,value="field.ui-field-contain")mins=0mins_one=0mins_two=0mins_three=0mins_four=0mins_five=0mins_six=0mins_seven=0mins_eight=0mins_night=0# print(len(rank))for i in range(0,len(rank)):type_q = rank[i].get_attribute("type")# print(type_q)# 单选if(type_q == '3'):if(i==15):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")mins = random.randint(0,len(check)-2)# print(type(check))check[mins].click()# check='null'elif(i==0):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1]weight = [0.6,0.4]choiceNum = random.choices(alist, weights=weight)mins = choiceNum[0]check[mins].click()# flag=True# check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")# # print(type(check))# while(flag):# mins = random.randint(0,len(check)-1)# # print(mins)# if(mins==1 or mins==2):# flag=True# # mins = random.randint(0,len(check)-1)# # check[mins].click()# else:# check[mins].click()# flag=Falseelif(i==1):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")#利用权重对选项进行选择alist = [0, 1, 2, 3, 4, 5]weight = [0,0,0.5,0.35,0.15,0]choiceNum = random.choices(alist, weights=weight)mins_one = choiceNum[0]check[mins_one].click()elif(i==2):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1, 2, 3, 4]weight = [0.2,0.5,0.3,0,0]choiceNum = random.choices(alist, weights=weight)mins_two = choiceNum[0]check[mins_two].click()elif(i==3):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")print(mins_one)if(mins_one==2):alist = [0, 1, 2, 3, 4,5,6,7]weight = [0,0.28,0.2,0.38,0.12,0.02,0,0]choiceNum = random.choices(alist, weights=weight)mins_three = choiceNum[0]check[mins_three].click()if(mins_one==3):alist = [0, 1, 2, 3, 4,5,6,7]weight = [0,0.2,0.36,0.24,0.18,0.02,0,0]choiceNum = random.choices(alist, weights=weight)mins_three = choiceNum[0]check[mins_three].click()if(mins_one==4):alist = [0, 1, 2, 3, 4,5,6,7]weight = [0,0.2,0.36,0.24,0.18,0.02,0,0]choiceNum = random.choices(alist, weights=weight)mins_three = choiceNum[0]check[mins_three].click()elif(i==4):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")if(mins_three==1):alist = [0, 1, 2, 3, 4,5,6]weight = [0,0.05,0.15,0.4,0.3,0.05,0.05]choiceNum = random.choices(alist, weights=weight)mins_four = choiceNum[0]check[mins_four].click()if(mins_three==2):alist = [0, 1, 2, 3, 4,5,6]weight = [0.02,0.1,0.2,0.4,0.3,0.1,0.1]choiceNum = random.choices(alist, weights=weight)mins_four = choiceNum[0]check[mins_four].click()if(mins_three==3):alist = [0, 1, 2, 3, 4,5,6]weight = [0,0.05,0.2,0.4,0.3,0.1,0.1]choiceNum = random.choices(alist, weights=weight)mins_four = choiceNum[0]check[mins_four].click()if(mins_three==4):mins_four=random.randint(0,6)check[mins_four].click()if(mins_three==5):mins_four=0check[mins_four].click()if(mins_three==6):mins_four=random.randint(0,1)check[mins_four].click()if(mins_three==7):mins_four=random.randint(0,6)check[mins_four].click()elif(i==5):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")if(mins==1):alist = [0, 1, 2, 3, 4,5,6,7,8]weight = [0.05,0.02,0.1,0.05,0.2,0.3,0.05,0.05,0.08]choiceNum = random.choices(alist, weights=weight)mins_five = choiceNum[0]check[mins_five].click()if(mins==0):alist = [0, 1, 2, 3, 4,5,6,7,8]weight = [0.12,0.3,0.12,0.12,0.1,0,0.12,0.1,0.02]choiceNum = random.choices(alist, weights=weight)mins_five = choiceNum[0]check[mins_five].click()elif(i==6):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1, 2, 3, 4]weight = [0.45,0.1,0.05,0.2,0.2]choiceNum = random.choices(alist, weights=weight)mins_six= choiceNum[0]check[mins_six].click()elif(i==7):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1, 2, 3, 4]weight = [0.3,0.4,0.15,0.1,0.05]choiceNum = random.choices(alist, weights=weight)mins_seven= choiceNum[0]check[mins_seven].click()elif(i==8):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")if(mins_seven==0):mins_eight=random.randint(0,4)check[mins_eight].click()if(mins_seven==1):mins_eight=random.randint(1,4)check[mins_eight].click()if(mins_seven==2):mins_eight=random.randint(2,4)check[mins_eight].click()if(mins_seven==3):mins_eight=random.randint(3,4)check[mins_eight].click()if(mins_seven==4):mins_eight=4check[mins_eight].click()elif(i==9):check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1, 2, 3, 4]weight = [0.01,0.6,0.3,0.07,0.02]choiceNum = random.choices(alist, weights=weight)mins_night= choiceNum[0]check[mins_night].click() # 多选if(type_q == '4'):ui_check = rank[i].find_elements(by=By.CLASS_NAME,value="label")length = len(ui_check)# print(length)for k in range(len(ui_check)):# 如果多选中填空题选项不叫其他,请更改此行代码if ui_check[k].text == '其他':length = length-1# 如果多选规定选择的项数请修改下面的random_choice = np.random.randint(0,length,size=random.randint(1,length))for z in list(set(random_choice)):ui_check[z].click()# 量表if(type_q == '5'):lb_data = rank[i].find_elements(by=By.CLASS_NAME,value="td")lb_data[random.randint(0,len(lb_data)-1)].click()# 矩阵量表if(type_q == '6'):# print(rank[i].text)if(i==13):mat_data = rank[i].find_elements(by=By.CSS_SELECTOR,value="tr[tp=d]")# print(len(mat_data))# mat_data = rank[i].find_elements_by_xpath("./tbody/tr[@tp=\"d\"]")for s in range(0,len(mat_data)):single_mat_row = mat_data[s].find_elements(by=By.CLASS_NAME,value='rate-off.rate-offlarge')# print(len(single_mat_row))# check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1, 2, 3, 4]weight = [0.5,0.3,0.3,0.15,0.05]choiceNum = random.choices(alist, weights=weight)[0]single_mat_row[choiceNum].click()elif(i==14):mat_data = rank[i].find_elements(by=By.CSS_SELECTOR,value="tr[tp=d]")# print(len(mat_data))# mat_data = rank[i].find_elements_by_xpath("./tbody/tr[@tp=\"d\"]")for s in range(0,len(mat_data)):single_mat_row = mat_data[s].find_elements(by=By.CLASS_NAME,value='rate-off.rate-offlarge')# print(len(single_mat_row))# check = rank[i].find_elements(by=By.CLASS_NAME,value="ui-radio")alist = [0, 1, 2, 3, 4]weight = [0.05,0.15,0.3,0.3,0.5]choiceNum = random.choices(alist, weights=weight)[0]single_mat_row[choiceNum].click()else:mat_data = rank[i].find_elements(by=By.CSS_SELECTOR,value="tr[tp=d]")# print(len(mat_data))# mat_data = rank[i].find_elements_by_xpath("./tbody/tr[@tp=\"d\"]")for s in range(0,len(mat_data)):single_mat_row = mat_data[s].find_elements(by=By.CLASS_NAME,value='rate-off.rate-offlarge')# print(len(single_mat_row))alist = [0, 1, 2, 3, 4]weight = [0.05,0.15,0.3,0.5,0.7]choiceNum = random.choices(alist, weights=weight)[0]# single_mat_row[choiceNum].click()single_mat_row[choiceNum].click()driver.find_element(by=By.ID,value="ctlNext").click()# XPATH='//*[@id="layui-layer1"]/div[3]/a[1]'# 消除确认弹窗time.sleep(random.randint(2, 5))# js = 'document.getElementById("alert_box").style.display="none";'driver.find_element(by=By.CLASS_NAME,value="layui-layer-btn0").click()# time.sleep(0.5)time.sleep(random.randint(3, 7))# driver.execute_script(js)# 点智能验证提示框,进行智能验证driver.find_element(by=By.XPATH,value="//div[@id='rectMask']").click()time.sleep(random.randint(2, 15))driver.quit() # 运行完毕自动关闭浏览器# driver.quit()if __name__ == '__main__':for i in range(1,1001):print("正在填写第{}份问卷".format(i))auto_write()print("第{}份问卷已经填写完成".format(i))time.sleep(random.randint(2,6))#此处需要停留20分钟,建议晚上挂机刷问卷if(i%20==0):time.sleep(1200)
最后的结果: