- 形势与政策要做调查问卷,正好之前爬虫学了selenium,就写了个自动刷的脚本,目前仅可刷单选,但是可以单独设定每题每个选项的概率
代码实现
- 如果直接拿selenium写,问卷星就会弹验证🐎,因为问卷星有做对selenium的检测,使用下面的代码可以简单过一部分检测
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(chrome_options=options)
- 这里使用了网上的一份p_random函数,传入两个list,第一个作为被选择的元素,第二个作为每个元素被选中的概率
def p_random(arr1,arr2):
assert len(arr1) == len(arr2), "Length does not match."
#assert sum(arr2) == 1 , "Total rate is not 1."
sup_list = [len(str(i).split(".")[-1]) for i in arr2]
top = 10 ** max(sup_list)
new_rate = [int(i*top) for i in arr2]
rate_arr = []
for i in range(1,len(new_rate)+1):
rate_arr.append(sum(new_rate[:i]))
rand = random.randint(1,top)
data = None
for i in range(len(rate_arr)):
if rand <= rate_arr[i]:
data = arr1[i]
break
return data
- 完整脚本如下,现在设置的11个问题(单选),可以自己按照格式添加或者删除元素,p_random第一个传入的每个选项的序号,第二个传入的每个选项的概率
from selenium import webdriver
import time
import random
def p_random(arr1,arr2):
assert len(arr1) == len(arr2), "Length does not match."
#assert sum(arr2) == 1 , "Total rate is not 1."
sup_list = [len(str(i).split(".")[-1]) for i in arr2]
top = 10 ** max(sup_list)
new_rate = [int(i*top) for i in arr2]
rate_arr = []
for i in range(1,len(new_rate)+1):
rate_arr.append(sum(new_rate[:i]))
rand = random.randint(1,top)
data = None
for i in range(len(rate_arr)):
if rand <= rate_arr[i]:
data = arr1[i]
break
return data
i=1
while 1:
print("第%s份问卷"%str(i))
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(chrome_options=options)
driver.get(r"https://www.wjx.cn/vm/QVd6t9l.aspx")
time.sleep(5)
#1
a=p_random([1,2],[0.5,0.5])
driver.find_element_by_xpath('//*[@id="div1"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#2
a=p_random([1,2,3,4],[0.1,0.3,0.1,0.5])
driver.find_element_by_xpath('//*[@id="div2"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#3
a=p_random([1,2,3,4],[0.5,0.1,0.3,0.1])
driver.find_element_by_xpath('//*[@id="div3"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#4
a=p_random([1,2,3,4],[0.4,0.4,0.1,0.1])
driver.find_element_by_xpath('//*[@id="div4"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#5
a=p_random([1,2,3],[0.3,0.5,0.2])
driver.find_element_by_xpath('//*[@id="div5"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#6
a=p_random([1,2,3],[0.1,0.3,0.6])
driver.find_element_by_xpath('//*[@id="div6"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#7
a=p_random([1,2,3],[0.1,0.1,0.8])
driver.find_element_by_xpath('//*[@id="div7"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#8
a=p_random([1,2,3,4],[0.1,0.4,0.4,0.1])
driver.find_element_by_xpath('//*[@id="div8"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#9
a=p_random([2,3,4],[0.4,0.4,0.2])
driver.find_element_by_xpath('//*[@id="div9"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#10
a=p_random([1,2,3],[0.2,0.2,0.6])
driver.find_element_by_xpath('//*[@id="div10"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
#11
a=p_random([1,2,3,4,5],[0.2,0.2,0.2,0.2,0.2])
driver.find_element_by_xpath('//*[@id="div11"]/div[2]/div[%s]'%str(a)).click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="ctlNext"]').click()
time.sleep(5)
driver.quit()
i=i+1