当前位置:   article > 正文

python问卷填写(填空,量表,矩阵题)可控比例_python 问卷星矩阵量表脚本

python 问卷星矩阵量表脚本

 选修课期末通知要200份问卷,许多事要忙,临时起意,怒学六小时,搞定问卷自动化填写

后续我会解释完善,等我结课

先附源码,需要自取

  1. from selenium.webdriver.common.by import By
  2. from selenium import webdriver #selenium库
  3. import random # 用于产生随机数
  4. import time # 用于延时
  5. import pyautogui # 用于模拟人手
  6. from selenium.webdriver.chrome.service import Service
  7. def gundong(driver, distance): #延时+屏幕滚动
  8. js = "var q=document.documentElement.scrollTop=" + str(distance) #下拉像素(800是基于最顶端测算的距离)
  9. driver.execute_script(js)
  10. time.sleep(0.5)
  11. def danxuan(driver,num):
  12. # 找到所有标签(定位问题)这里是单选
  13. # dan = driver.find_elements_by_css_selector('#div1 >'+ str(path))
  14. w=[[0.88,0.12],[0.69,0.31],[0.79,0.21],[0.10,0.64,0.09,0.097,0.073],[0.05,0.21,0.74]]
  15. for index,n_um in enumerate(num):
  16. dans = driver.find_element(by=By.ID, value=f'div{n_um}')
  17. dan=dans.find_elements(by=By.CLASS_NAME,value='ui-radio')
  18. l_ist=random.choices(dan,weights=w[index],k=1)
  19. print(l_ist)
  20. for i in l_ist:
  21. i.click()#找到标签并点击#choice()与choices()有区别的
  22. time.sleep(random.randint(0, 1))
  23. def duoxuan(driver,num):
  24. #num是题目数量
  25. #这里是多选题,找到所有多选的标签
  26. w=[[0.51,0.31,0.18]]
  27. for index, n_um in enumerate(num):
  28. duo = driver.find_element_by_css_selector(f'#div{n_um} > div.ui-controlgroup.column1')
  29. ans = duo.find_elements_by_css_selector('.ui-checkbox') # 对应的绝对子标签
  30. k1 = random.randint(1, len(w[index]))#多选题随机填的个数
  31. l_ist = random.choices(ans, weights=w[0], k=k1) # 相当于l_ist表里存的是各个选项的位置
  32. for i in l_ist:
  33. i.click() # 找到标签并点击
  34. time.sleep(random.randint(0, 1))
  35. time.sleep(random.randint(0, 1))
  36. def tiankong(driver, num,index,answer):
  37. #填空函数
  38. for n_um in num:
  39. tians = driver.find_element(by=By.ID, value=f'drv1_{n_um}')
  40. tian = tians.find_element(by=By.ID, value=f'q1_{n_um-1}')
  41. tian.send_keys(answer.get(index[random.randint(0, len(index) - 1)]))
  42. time.sleep(random.randint(0, 1))
  43. def jvzheng(driver,num):
  44. w=[[0.09,0.18,0.73],[0.015,0.096,0.889],[0.096,0.09,0.814]]
  45. for index, n_um in enumerate(num):
  46. jvs = driver.find_element(by=By.ID, value=f'drv2_{n_um}')
  47. jv=jvs.find_elements(by=By.CSS_SELECTOR, value='a')
  48. l_ist = random.choices(jv, weights=w[index], k=1) # 相当于l_ist表里存的是各个选项的位置
  49. for i in l_ist:
  50. i.click()
  51. time.sleep(random.randint(0, 1))
  52. time.sleep(random.randint(0, 1))
  53. def liangbiao(driver,num):
  54. w=[[0.011,0.08,0.64,0.16,0.109]]
  55. for index, n_um in enumerate(num):
  56. liangs = driver.find_element(by=By.ID, value=f'drv{n_um}_1')
  57. liang = liangs.find_elements(by=By.CSS_SELECTOR, value='a')
  58. l_ist = random.choices(liang, weights=w[index], k=1) # 相当于l_ist表里存的是各个选项的位置
  59. for i in l_ist:
  60. i.click() # 找到标签并点击
  61. time.sleep(random.randint(0, 1))
  62. time.sleep(random.randint(0, 1))
  63. def renzheng(driver):
  64. # 智能验证,找到智能认证的标签
  65. bth = driver.find_element_by_css_selector('#layui-layer1 > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0')
  66. bth.click()#点击
  67. time.sleep(4)
  68. rectBottom = driver.find_element_by_css_selector('#rectBottom') #提交按钮
  69. rectBottom.click() #点击
  70. time.sleep(5)
  71. def huakuai():
  72. # 当次数多了的时候就会出现滑块,这里是模拟人手解决滑块拖动
  73. pyautogui.moveTo(random.randint(494, 496), 791, 0.2)#控制鼠标移动到x,y处,耗时0.2秒
  74. time.sleep(1)
  75. pyautogui.dragTo(random.randint(888, 890), 791, 1)#让鼠标点击并拖拽到x,y处,耗时1秒
  76. time.sleep(1)
  77. pyautogui.click(random.randint(652, 667), random.randint(793, 795))#让鼠标点击x,y处
  78. time.sleep(1)
  79. pyautogui.moveTo(random.randint(494, 496), 791, 0.2)#控制鼠标移动到x,y处,耗时0.2秒
  80. time.sleep(1)
  81. pyautogui.dragTo(random.randint(888, 890), 791, 1)#让鼠标点击并拖拽到x,y处,耗时1秒
  82. def zonghe(times):
  83. for i in range(0, times):
  84. # 初始配置,地址
  85. url_survey = 'https://www.wjx.cn/vm/ODAvYPc.aspx '
  86. option = webdriver.ChromeOptions()
  87. option.add_experimental_option('excludeSwitches', ['enable-automation'])
  88. option.add_experimental_option('useAutomationExtension', False)
  89. # 本地下载的谷歌浏览器地址
  90. option.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
  91. # 下载好的Chrome驱动的地址
  92. s = Service("chromedriver_win32/chromedriver.exe")
  93. driver = webdriver.Chrome(service=s)
  94. driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
  95. {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
  96. with open("明星.txt", "r", encoding="utf-8") as f:
  97. an_swer = f.readlines()
  98. an_swer = [line.strip("\n") for line in an_swer]
  99. answer={}
  100. for index, value in enumerate(an_swer):
  101. answer[index]=value
  102. index=[i for i in range(0,42)]
  103. # 启动要填写的地址
  104. driver.get(url_survey)
  105. tiankong(driver, [1,2,3],index,answer) # 调用填空题函数
  106. jvzheng(driver, [1,2,3])
  107. danxuan(driver, [3, 4, 5, 8, 9]) # 调用单选函数#[]里是位置标志数字
  108. try:
  109. liangbiao(driver,[6])
  110. duoxuan(driver,[7]) # 调用多选函数
  111. gundong(driver, 600) # 调用滚动屏幕函数,如果不需要则注释掉
  112. except:
  113. print("直接跳到问卷结束提交")
  114. # 最后交卷点击提交
  115. time.sleep(random.randint(0, 1))
  116. # driver.find_element_by_xpath('//*[@id="divContent"]').click()
  117. # 找到提交的css并点击
  118. # button=driver.find_element_by_css_selector('#ctlNext')
  119. # webdriver.ActionChains(driver).move_to_element(button).click(button).perform()
  120. try:
  121. driver.find_element_by_css_selector('#ctlNext').click()
  122. except:
  123. print("无法提交")
  124. time.sleep(4)
  125. try:
  126. renzheng(driver) # 智能认证函数调用
  127. huakuai() # 滑块函数调用
  128. except:
  129. print("未出现验证")
  130. print('已经提交了{}次问卷'.format(int(i) + int(1)))
  131. time.sleep(4)
  132. driver.quit()#停止
  133. if __name__ == "__main__":
  134. zonghe(170)#里面填写的数是表示要提交多少次问卷

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/605961
推荐阅读
相关标签
  

闽ICP备14008679号