版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yan_star/article/details/89848105
Python实现问卷星自动填写和选择
昨天,朋友找我填个问卷作为毕业论文数据支撑,但是无奈根本没人填写,只好他自己一份一份的填写,数据也都是怎么合适自己的论文写作怎么来,我填了一份就想吐了,只好祭出我的大Python,帮他搞定吧,谁叫我乐于助人哈哈哈!电话沟通好需求,我又给他加了些数据上下关联,开搞!
主要采用Python+Selenium来实现
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time import random ''' 要求: 年龄:28-50 男 排除小学 人口3-6,在家2-4,上学 0-10 否 是 3-4 范围:比较,完全 销量:比较,完全 成本:中间3 太复杂:比较不同意,比较同意 人才:中间3 政府:中间3 ''' def run(): driver = webdriver.Firefox() driver.get("https://www.wjx.cn/jq/33556010.aspx") time.sleep(2) #question1 randomId1 = random.randint(28,45) driver.find_element_by_id("q1").send_keys(randomId1) #question2 js = 'document.getElementById("q2_1").click()' driver.execute_script(js) #question3 大于35岁没上过大学 if(randomId1 >= 35): randomId3 = "q3_" + str(random.randint(1,4)) js = 'document.getElementById("'+randomId3+'").click()' driver.execute_script(js) #小于35岁没低于小学 else: randomId3 = "q3_" + str(random.randint(2, 5)) js = 'document.getElementById("' + randomId3 + '").click()' driver.execute_script(js) #question4 randomId4_1 = random.randint(3,6) #6口人,3\4在家,3\2人上学 if(randomId4_1 == 6): randomId4_2 = random.randint(3, 4) randomId4_3 = randomId4_1 - randomId4_2 driver.find_element_by_id("q4_1").send_keys(randomId4_1) driver.find_element_by_id("q4_2").send_keys(randomId4_2) driver.find_element_by_id("q4_3").send_keys(randomId4_3) #3口人,1\2在家,1\2人上学 elif(randomId4_1 == 3): randomId4_2 = random.randint(1,2) randomId4_3 = randomId4_1 - randomId4_2 driver.find_element_by_id("q4_1").send_keys(randomId4_1) driver.find_element_by_id("q4_2").send_keys(randomId4_2) driver.find_element_by_id("q4_3").send_keys(randomId4_3) else: randomId4_2 = random.randint(2,4) randomId4_3 = randomId4_1 - randomId4_2 driver.find_element_by_id("q4_1").send_keys(randomId4_1) driver.find_element_by_id("q4_2").send_keys(randomId4_2) driver.find_element_by_id("q4_3").send_keys(randomId4_3) #question5 randomId5 = random.randint(0,10) driver.find_element_by_id("q5").send_keys(randomId5) #question6 js = 'document.getElementById("q6_2").click()' driver.execute_script(js) #question7 #randomId7 = "q7_" + str(random.randint(1,2)) js = 'document.getElementById("q7_1").click()' driver.execute_script(js) #question8 randomId8 = random.randint(3,4) driver.find_element_by_id("q8").send_keys(randomId8) #question9_0 randomId9_0 = str(random.randint(3,4)) js = 'document.getElementsByName("q9_0")['+randomId9_0+'].click()' driver.execute_script(js) #question9_1 randomId9_1 = str(random.randint(3,4)) js = 'document.getElementsByName("q9_1")['+randomId9_1+'].click()' driver.execute_script(js) #question9_2 randomId9_2 = str(random.randint(1,3)) js = 'document.getElementsByName("q9_2")['+randomId9_2+'].click()' driver.execute_script(js) #question9_3 randomId9_3 = str(random.randint(3,4)) js = 'document.getElementsByName("q9_3")['+randomId9_3+'].click()' driver.execute_script(js) #question9_4 randomId9_4 = str(random.randint(3,4)) js = 'document.getElementsByName("q9_4")['+randomId9_4+'].click()' driver.execute_script(js) #question10_0 randomId10_0 = str(random.randint(3,4)) js = 'document.getElementsByName("q10_0")['+randomId10_0+'].click()' driver.execute_script(js) #question10_1 list_id = [1,3] randomId10_01 = random.randint(0,1) randomId10_11 = str(list_id[randomId10_01]) js = 'document.getElementsByName("q10_1")['+randomId10_11+'].click()' driver.execute_script(js) #question10_2 randomId10_2 = str(random.randint(3,4)) js = 'document.getElementsByName("q10_2")['+randomId10_2+'].click()' driver.execute_script(js) #question11_0 randomId11_0 = str(random.randint(3,4)) js = 'document.getElementsByName("q11_0")['+randomId11_0+'].click()' driver.execute_script(js) #question11_1 randomId11_1 = str(random.randint(1,3)) js = 'document.getElementsByName("q11_1")['+randomId11_1+'].click()' driver.execute_script(js) #question11_2 randomId11_2 = str(random.randint(3,4)) js = 'document.getElementsByName("q11_2")['+randomId11_2+'].click()' driver.execute_script(js) #question12_0 randomId12_0 = str(random.randint(3,4)) js = 'document.getElementsByName("q12_0")['+randomId12_0+'].click()' driver.execute_script(js) #question12_1 randomId12_1 = str(random.randint(3,4)) js = 'document.getElementsByName("q12_1")['+randomId12_1+'].click()' driver.execute_script(js) #question12_2 randomId12_2 = str(random.randint(3,4)) js = 'document.getElementsByName("q12_2")['+randomId12_2+'].click()' driver.execute_script(js) #question12_3 randomId12_3 = str(random.randint(3,4)) js = 'document.getElementsByName("q12_3")['+randomId12_3+'].click()' driver.execute_script(js) #question13_0 randomId13_0 = str(random.randint(3,4)) js = 'document.getElementsByName("q13_0")['+randomId13_0+'].click()' driver.execute_script(js) #question13_1 randomId13_1 = str(random.randint(3,4)) js = 'document.getElementsByName("q13_1")['+randomId13_1+'].click()' driver.execute_script(js) #question13_2 randomId13_2 = str(random.randint(3,4)) js = 'document.getElementsByName("q13_2")['+randomId13_2+'].click()' driver.execute_script(js) #question14_0 randomId14_0 = str(random.randint(1,3)) js = 'document.getElementsByName("q14_0")['+randomId14_0+'].click()' driver.execute_script(js) #question14_1 randomId14_1 = str(random.randint(1,3)) js = 'document.getElementsByName("q14_1")['+randomId14_1+'].click()' driver.execute_script(js) #question14_2 randomId14_2 = str(random.randint(1,3)) js = 'document.getElementsByName("q14_2")['+randomId14_2+'].click()' driver.execute_script(js) #question14_3 randomId14_3 = str(random.randint(1,3)) js = 'document.getElementsByName("q14_3")['+randomId14_3+'].click()' driver.execute_script(js) #question15_0 randomId15_0 = str(random.randint(3,4)) js = 'document.getElementsByName("q15_0")['+randomId15_0+'].click()' driver.execute_script(js) #question15_1 randomId15_1 = str(random.randint(3,4)) js = 'document.getElementsByName("q15_1")['+randomId15_1+'].click()' driver.execute_script(js) #question15_2 randomId15_2 = str(random.randint(3,4)) js = 'document.getElementsByName("q15_2")['+randomId15_2+'].click()' driver.execute_script(js) time.sleep(2) #time.sleep(random.randint(90,150)) #driver.find_element_by_id("submit_button").click() # time.sleep(3) #driver.quit() if __name__ == '__main__': for i in range(1,2): run() print(i)
有两个难点:
1、网页元素设置为了隐藏,selenium获取不到,这时候采用的是JS语句执行来帮助我们
2、确定目标,'document.getElementsByName("q9_1")['+randomId9_1+'].click()',getElementsByName得到的是一个列表,通过列表来定位,此次适用于多个单选框题型
最后,帮助他完成了150份问卷,关于IP封禁和验证码问题我没有处理,可能比较懒吧,之前处理过类似问题,一种是采用代理池的方式,还可以调用百度之类的识别库对验证码进行识别,识别的话可以把图片保存到本地,然后识别返回结果,我github上有项目代码(https://github.com/YanStar)感兴趣可以翻阅一下,有什么问题,欢迎大家前来交流!
文章来源: https://blog.csdn.net/yan_star/article/details/89848105