当前位置:   article > 正文

最新python实现问卷星刷问卷,Edge版本_python刷问卷星

python刷问卷星

最近朋友找我写问卷,我想到我的朋友最近一直在搞刷问卷(智能验证的代码就是抄的他的),心想求人不如求己,爆肝两天做出了此代码。

代码实现单选多选填空下拉框量表矩阵题型的回答,其实别的题型原理类似,只不过市面基本就这些类型了。每个题目可以自行设置概率,(多选题较为复杂 平常基本不用 我注释了)

因为自己平常喜欢用edge 加上目前谷歌版本的代码已经很全了,就把我edge版本的代码放了上来(其实就是换名称而已)

  1. from selenium import webdriver
  2. import time
  3. from selenium.webdriver.edge.service import Service
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. import random
  6. from selenium.webdriver.common.by import By
  7. """
  8. 仅仅作为测试和自我学习使用,请不要用于学术作弊或者其他违法行为。
  9. 目前没有发现验证问题,涵盖市面90%主流题型,后续会更新ip地址的修改
  10. 如果想学习或者有操作的问题可以联系我
  11. """
  12. # 滚动页面
  13. def scroll(driver, distance):
  14. js = f"var q=document.documentElement.scrollTop={distance}"
  15. driver.execute_script(js)
  16. # 单选题
  17. def single_choice(driver,num):
  18. # 找到所有单选题
  19. questions = driver.find_elements(By.CSS_SELECTOR, f'#div{num} > div.ui-controlgroup.column1')
  20. for question in questions:
  21. options = question.find_elements(By.CSS_SELECTOR, '.ui-radio')
  22. weights = [3, 7] # 第一个选项权重为7,第二个选项权重为3
  23. selected_option = random.choices(options, weights=weights)[0]
  24. selected_option.click()
  25. time.sleep(random.randint(0, 1))
  26. # 下拉框
  27. def xialakuang(driver,num):
  28. # 找到下拉框元素
  29. driver.find_element(By.XPATH, f'//*[@id="select2-q{num}-container"]').click()
  30. # 获取所有选项
  31. options = driver.find_elements(By.CSS_SELECTOR, '.select2-results__option')
  32. weights = [0,0.1,0.9] #默认第一个“请选择”不选,后面的为概率
  33. # 选择一个随机的选项
  34. random_option = random.choices(options, weights=weights)[0]
  35. # 点击选项
  36. random_option.click()
  37. time.sleep(random.randint(0, 1))
  38. # 多选题
  39. def multiple_choice(driver, num):
  40. # def multiple_choice(driver, num): 当要求设置组合概率时使用
  41. # # 找到所有多选题
  42. # questions = driver.find_elements(By.CSS_SELECTOR, f'#div{num} > div.ui-controlgroup.column1')
  43. # for question in questions:
  44. # options = question.find_elements(By.CSS_SELECTOR, '.ui-checkbox')
  45. #
  46. # # 为不同的选项组合设置不同的权重
  47. # weighted_options = [
  48. # (('A', 'B'), 10),
  49. # (('B',), 90),
  50. # ]
  51. #
  52. # # 使用权重随机选择一个选项组合
  53. # selected_option_combination = \
  54. # random.choices(weighted_options, weights=[w for _, w in weighted_options], k=1)[0][0]
  55. #
  56. # # 根据选择的选项组合选择选项
  57. # for i, option in enumerate(options):
  58. # option_text = option.text
  59. # if option_text in selected_option_combination:
  60. # if not option.is_selected():
  61. # option.click()
  62. # time.sleep(random.randint(0, 1))
  63. # 一般就用这个
  64. questions = driver.find_elements(By.CSS_SELECTOR, '#div2 > div.ui-controlgroup.column1')
  65. for question in questions:
  66. options = question.find_elements(By.CSS_SELECTOR, '.ui-checkbox')
  67. num_choices = random.randint(1, len(options))
  68. selected_options = random.sample(options, num_choices)
  69. for option in selected_options:
  70. if not option.is_selected():
  71. option.click()
  72. time.sleep(random.randint(0, 1))
  73. # 填空题
  74. def fill_in_the_blank(driver, num):
  75. index = ["A", "B", "C"]
  76. answers = {"A": "python真的好用!", "B": "这个测试很成功!", "C": "填空题随机填写文本"}
  77. weights = [0, 0.5, 0.5] # 按照顺序给每个答案分配权重值
  78. # 根据权重值选择答案
  79. selected_answer = random.choices(list(answers.values()), weights=weights)[0]
  80. # 填写相应的答案
  81. driver.find_element(By.CSS_SELECTOR, f'#q{num}').send_keys(selected_answer)
  82. time.sleep(random.randint(0, 1))
  83. # 矩阵题
  84. def juzhen(driver, num):
  85. num_dimensions = 2 # 设置维度的数量,根据实际情况进行调整
  86. weights_list = [
  87. [0, 0, 0,0.5,0.5], # 第一个维度的选项权重列表
  88. [0.5, 0.5, 0,0,0] # 第二个维度的选项权重列表
  89. ]
  90. for i in range(1, num_dimensions + 1):
  91. options = driver.find_elements(By.XPATH, f'//*[@id="drv{num}_{i}"]/td/a')
  92. weights = weights_list[i - 1] # 根据当前维度的索引选择对应的权重列表
  93. selected_option = random.choices(options, weights=weights)[0]
  94. selected_option.click()
  95. time.sleep(random.randint(0, 1))
  96. # 两表题
  97. def liangbiao(driver, num):
  98. options = driver.find_elements(By.XPATH, f'//*[@id="div{num}"]/div[2]/div/ul/li')
  99. weights = [0, 0, 0,0.5,0.5] # 分配选项的权重值,按照顺序与options列表对应
  100. selected_option = random.choices(options, weights=weights)[0]
  101. selected_option.click()
  102. time.sleep(random.randint(0, 1))
  103. # 解决验证
  104. def yanzheng(driver):
  105. try:
  106. time.sleep(1) # 等待验证弹出
  107. # 点击对话框的确认按钮
  108. driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a').click()
  109. time.sleep(4) # 确认验证时间
  110. # 点击智能检测按钮
  111. driver.find_element(By.XPATH, '//*[@id="SM_BTN_1"]').click()
  112. time.sleep(4) # 智能监测时间
  113. except:
  114. print("无验证")
  115. # 滑块验证
  116. # 目前来看滑块验证出现的概率很低,出现了一般可以直接关闭服务重新刷新来解决,还未遇到出现滑块的状况
  117. try:
  118. slider = driver.find_element(By.XPATH, '//*[@id="nc_1__scale_text"]/span')
  119. if str(slider.text).startswith("请按住滑块"):
  120. width = slider.size.get('width')
  121. ActionChains(driver).drag_and_drop_by_offset(slider, width, 0).perform()
  122. except:
  123. pass
  124. # 启动!
  125. def qd(times):
  126. url = 'https://www.wjx.cn/vm/e8pnohT.aspx# ' # 传入问卷星的链接
  127. # driver.set_window_size(600, 400) #设置窗口大小
  128. driver_path = r"C:\Users\lenovo\Music\edgedriver_win64\msedgedriver.exe" # 传入edgedriver的路径
  129. options = webdriver.EdgeOptions()
  130. options.add_argument("--disable-blink-features=AutomationControlled")
  131. options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" # 传入edge的路径
  132. for i in range(times):
  133. # 躲避智能检测,将webDriver设置为false
  134. option = webdriver.EdgeOptions()
  135. option.add_experimental_option('excludeSwitches', ['enable-automation'])
  136. option.add_experimental_option('useAutomationExtension', False)
  137. service = Service(driver_path)
  138. service.start()
  139. driver = webdriver.Edge(service=service, options=options)
  140. try:
  141. driver.get(url)
  142. #运行区
  143. single_choice(driver, 1)
  144. multiple_choice(driver, 2)
  145. fill_in_the_blank(driver, 3)
  146. xialakuang(driver, 4)
  147. scroll(driver, 300)
  148. juzhen(driver, 5)
  149. scroll(driver, 600)
  150. liangbiao(driver,6)
  151. time.sleep(random.randint(0, 1))
  152. driver.find_element(By.CSS_SELECTOR, '#ctlNext').click()
  153. yanzheng(driver)
  154. time.sleep(2)
  155. print(f'已经提交了{i + 1}次问卷')
  156. except Exception as e:
  157. print(f'出现异常:{str(e)}')
  158. finally:
  159. driver.quit()
  160. service.stop()
  161. if __name__ == "__main__":
  162. qd(5)

这是我最初跟着学习的帖子(47条消息) 用Python实现问卷星自动填写(超详细!!!)_python自动填写问卷_不当王多鱼不改名的博客-CSDN博客

这是我好兄弟写的 他的代码非常完善(虽然感觉我的更好看懂)加入了ip更改,我的验证功能是在他的功能上改的

(47条消息) python+selenium,刷问卷星,支持ip+比例,无需改代码,设置ip+比例即可运行_black^sugar的博客-CSDN博客

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

闽ICP备14008679号