当前位置:   article > 正文

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

python刷腾讯问卷

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

1 下载依赖selenium

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

2  安装chrome驱动

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

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

 

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

 

  3 导入相关的包

  1. from selenium import webdriver
  2. import time
  3. import random
  4. from selenium.webdriver.common.by import By
  5. import pyautogui

防止被识别为脚本

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

  

代码如下

  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/1/26 11:34
  3. # @Author : wkk
  4. # @File : Python自动化填问卷星.py
  5. from selenium import webdriver
  6. import time
  7. import random
  8. from selenium.webdriver.common.by import By
  9. import pyautogui
  10. def run(num, url):
  11. for i in range(num):
  12. option = webdriver.ChromeOptions()
  13. option.add_experimental_option('excludeSwitches', ['enable-automation'])
  14. option.add_experimental_option('useAutomationExtension', False)
  15. browser = webdriver.Chrome(options=option)
  16. browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
  17. {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
  18. browser.maximize_window() # 窗口最大化
  19. # 这行代码的作用是将webdriver这个属性置为undefined
  20. browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
  21. {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
  22. })
  23. browser.get(url)
  24. time.sleep(2)
  25. num1 = random.randint(1, 2)
  26. if browser.find_element(by=By.XPATH, value=f"//div[@for='q1_{num1}']"):
  27. browser.find_element(by=By.XPATH, value=f"//div[@for='q1_{num1}']").click()
  28. time.sleep(0.5)
  29. num2 = random.randint(1, 3)
  30. if browser.find_element(by=By.XPATH, value=f"//div[@for='q2_{num2}']"):
  31. browser.find_element(by=By.XPATH, value=f"//div[@for='q2_{num2}']").click()
  32. time.sleep(0.5)
  33. num3 = random.randint(1, 3)
  34. if browser.find_element(by=By.XPATH, value=f"//div[@for='q3_{num3}']"):
  35. browser.find_element(by=By.XPATH, value=f"//div[@for='q3_{num3}']").click()
  36. time.sleep(0.5)
  37. num4 = random.randint(1, 3)
  38. if browser.find_element(by=By.XPATH, value=f"//div[@for='q4_{num4}']"):
  39. browser.find_element(by=By.XPATH, value=f"//div[@for='q4_{num4}']").click()
  40. time.sleep(0.5)
  41. num5 = random.randint(1, 2)
  42. if browser.find_element(by=By.XPATH, value=f"//div[@for='q5_{num5}']"):
  43. browser.find_element(by=By.XPATH, value=f"//div[@for='q5_{num5}']").click()
  44. time.sleep(0.5)
  45. num6 = random.randint(1, 3)
  46. if browser.find_element(by=By.XPATH, value=f"//div[@for='q6_{num6}']"):
  47. browser.find_element(by=By.XPATH, value=f"//div[@for='q6_{num6}']").click()
  48. time.sleep(0.5)
  49. num7 = random.randint(1, 4)
  50. if browser.find_element(by=By.XPATH, value=f"//div[@for='q7_{num7}']"):
  51. browser.find_element(by=By.XPATH, value=f"//div[@for='q7_{num7}']").click()
  52. time.sleep(0.5)
  53. # num8 = random.randint(1, 3)
  54. # if browser.find_element(by=By.XPATH, value=f"//div[@for='q8_{num8}']"):
  55. # browser.find_element(by=By.XPATH, value=f"//div[@for='q8_{num8}']").click()
  56. # time.sleep(0.5)
  57. # 问题5
  58. randomId = random.randint(1, 3) # 随机数选择(选多少个)
  59. for i in range(1, randomId + 1): # 循环 实现多选效果
  60. randomId1 = random.randint(1, 3) # 随机选择第1到第6个选项之一
  61. # 两种js实现方式
  62. js = "document.getElementById(\"q8_" + str(randomId1) + "\").checked = true"
  63. browser.execute_script(js)
  64. # 延时
  65. time.sleep(1)
  66. num9 = random.randint(1, 7)
  67. if browser.find_element(by=By.XPATH, value=f"//div[@for='q9_{num9}']"):
  68. browser.find_element(by=By.XPATH, value=f"//div[@for='q9_{num9}']").click()
  69. time.sleep(0.5)
  70. num10 = random.randint(1, 3)
  71. if browser.find_element(by=By.XPATH, value=f"//div[@for='q10_{num10}']"):
  72. browser.find_element(by=By.XPATH, value=f"//div[@for='q10_{num10}']").click()
  73. time.sleep(0.5)
  74. if browser.find_element(by=By.XPATH, value="//div[@class='submitbtn mainBgColor']"):
  75. browser.find_element(by=By.XPATH, value="//div[@class='submitbtn mainBgColor']").click()
  76. time.sleep(1)
  77. try:
  78. element = browser.find_element(By.CSS_SELECTOR, "#alert_box > div:nth-child(2) > div:nth-child(2) > button")
  79. element.click()
  80. time.sleep(1)
  81. yanz = browser.find_element(By.ID, "rectMask")
  82. yanz.click()
  83. time.sleep(4)
  84. except:
  85. pass
  86. # 先点确认
  87. try:
  88. browser.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
  89. time.sleep(1)
  90. except:
  91. pass
  92. # 再点智能验证提示框,进行智能验证
  93. try:
  94. browser.find_element(By.XPATH, '//*[@id="SM_BTN_WRAPPER_1"]').click()
  95. time.sleep(3)
  96. except:
  97. pass
  98. # 滑块验证
  99. pyautogui.moveTo(789, 805, duration=1) # 传入屏幕的位置
  100. time.sleep(0.5)
  101. pyautogui.dragRel(370, 0, duration=0.8) # 拖动
  102. # pyautogui.moveTo(789, 805, duration=1) # 回到原处
  103. # pyautogui.moveRel(370, 0, duration=1)
  104. # pyautogui.dragRel(256, 0, duration=0.8)
  105. # pyautogui.moveTo(789, 805)
  106. # time.sleep(0.5)
  107. # pyautogui.dragRel(370, 0, duration=0.8)
  108. time.sleep(5)
  109. browser.close()
  110. print(f"第{i + 1}份已经完成")
  111. if __name__ == '__main__':
  112. num = 10 # 填的份数
  113. url = 'https://www.wjx.cn/vm/tXQkrbD.aspx' # 链接
  114. run(num, url)

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

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

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

闽ICP备14008679号