此篇博客参考于:作者 微笑月亮: Python爬虫自动刷“问卷网”问卷(不锁IP).在上述参考代码下,根据自己的问卷情况改改就行了,超级简单。
1.首先你要去 这里: chome驱动下载传送门. 下载好你对应chome浏览器版本的chome驱动.
比如我的浏览器版本是83.0.4103.61
然后我就去下载了一个看上去和我版本最接近的的83.0.4103.39的驱动,如下:
2.如何更改参考代码成为符合自己要求的刷问卷工具呢?
先附上我改之后的完整代码。
from selenium import webdriver
import random
import time
option = webdriver.ChromeOptions()
option.add_argument('headless')
url = 'https://www.wenjuan.com/s/vMvi6b3/'
t = 200
# 设置提交问卷次数
path = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
for times in range(t):
driver = webdriver.Chrome(executable_path=path,options=option)
driver.get(url)
# 定位所有的问卷问题
questions = driver.find_elements_by_css_selector('.matrix')
for index,answers in enumerate(questions):
# 定位所有问卷问题选项
answer = answers.find_elements_by_css_selector('.icheckbox_div')
# 定位需要填写文字的选项,并填入相关内容
if not answer:
blank_potion = answers.find_element_by_css_selector('.blank.option')
blank_potion.send_keys('很好')
continue
# 单选题处理
if index == 1 or index == 5 or index == 7 or index == 18:
choose_ans = answer[random.randint(0, 3)]
choose_ans.click()
time.sleep(random.randint(0, 1))
elif index == 2 or index == 4 or index == 8:
choose_ans = answer[random.randint(0, 2)]
choose_ans.click()
time.sleep(random.randint(0, 1))
elif index == 9:
choose_ans = answer[random.randint(0, 5)]
choose_ans.click()
time.sleep(random.randint(0, 1))
# 多选题处理
elif index == 11 or index == 12 or index == 13 or index == 14 or index == 15 or index == 17:
for i in range(1, random.randint(2, 3)):
choose_ans = answer[random.randint(0, 4)]
choose_ans.click()
time.sleep(random.randint(0, 1))
# 双选题处理
else:
choose_ans = random.choice(answer)
choose_ans.click()
time.sleep(random.randint(0, 1))
subumit_button = driver.find_element_by_css_selector('#next_button')
subumit_button.click()
print('已经成功提交了{}次问卷'.format(int(times) + int(1)))
# 延迟问卷结果提交时间,以免间隔时间太短而无法提交
time.sleep(4)
driver.quit()
下面是教程:
最后要注意:
1.可能会出现自动填漏题的现象,正常。大部分都不会漏,放心。
如果想保证它不漏题,可以每个问题都设置一个else if确保万无一失。
2.可能会出现一开始把代码已经运行到可以自动答题了,然后突然又不可以了,可能是因为你的chome浏览器自动升级导致和chome驱动不匹配,最直接的方法就是再去下载一个对应的驱动,还附上一篇解决此问题的博客:让chome浏览器停止自动更新,彻底解决这个问题。链接: 如何关闭谷歌浏览器的自动更新?.