赞
踩
选修课期末通知要200份问卷,许多事要忙,临时起意,怒学六小时,搞定问卷自动化填写
后续我会解释完善,等我结课
先附源码,需要自取
- from selenium.webdriver.common.by import By
- from selenium import webdriver #selenium库
- import random # 用于产生随机数
- import time # 用于延时
- import pyautogui # 用于模拟人手
- from selenium.webdriver.chrome.service import Service
-
-
- def gundong(driver, distance): #延时+屏幕滚动
- js = "var q=document.documentElement.scrollTop=" + str(distance) #下拉像素(800是基于最顶端测算的距离)
- driver.execute_script(js)
- time.sleep(0.5)
-
- def danxuan(driver,num):
- # 找到所有标签(定位问题)这里是单选
- # dan = driver.find_elements_by_css_selector('#div1 >'+ str(path))
- w=[[0.88,0.12],[0.69,0.31],[0.79,0.21],[0.10,0.64,0.09,0.097,0.073],[0.05,0.21,0.74]]
- for index,n_um in enumerate(num):
- dans = driver.find_element(by=By.ID, value=f'div{n_um}')
- dan=dans.find_elements(by=By.CLASS_NAME,value='ui-radio')
- l_ist=random.choices(dan,weights=w[index],k=1)
- print(l_ist)
- for i in l_ist:
- i.click()#找到标签并点击#choice()与choices()有区别的
- time.sleep(random.randint(0, 1))
- def duoxuan(driver,num):
- #num是题目数量
- #这里是多选题,找到所有多选的标签
- w=[[0.51,0.31,0.18]]
- for index, n_um in enumerate(num):
- duo = driver.find_element_by_css_selector(f'#div{n_um} > div.ui-controlgroup.column1')
- ans = duo.find_elements_by_css_selector('.ui-checkbox') # 对应的绝对子标签
- k1 = random.randint(1, len(w[index]))#多选题随机填的个数
- l_ist = random.choices(ans, weights=w[0], k=k1) # 相当于l_ist表里存的是各个选项的位置
- for i in l_ist:
- i.click() # 找到标签并点击
- time.sleep(random.randint(0, 1))
- time.sleep(random.randint(0, 1))
-
- def tiankong(driver, num,index,answer):
- #填空函数
-
- for n_um in num:
- tians = driver.find_element(by=By.ID, value=f'drv1_{n_um}')
- tian = tians.find_element(by=By.ID, value=f'q1_{n_um-1}')
- tian.send_keys(answer.get(index[random.randint(0, len(index) - 1)]))
- time.sleep(random.randint(0, 1))
-
- def jvzheng(driver,num):
- w=[[0.09,0.18,0.73],[0.015,0.096,0.889],[0.096,0.09,0.814]]
- for index, n_um in enumerate(num):
- jvs = driver.find_element(by=By.ID, value=f'drv2_{n_um}')
- jv=jvs.find_elements(by=By.CSS_SELECTOR, value='a')
- l_ist = random.choices(jv, weights=w[index], k=1) # 相当于l_ist表里存的是各个选项的位置
- for i in l_ist:
- i.click()
- time.sleep(random.randint(0, 1))
- time.sleep(random.randint(0, 1))
-
- def liangbiao(driver,num):
- w=[[0.011,0.08,0.64,0.16,0.109]]
- for index, n_um in enumerate(num):
- liangs = driver.find_element(by=By.ID, value=f'drv{n_um}_1')
- liang = liangs.find_elements(by=By.CSS_SELECTOR, value='a')
- l_ist = random.choices(liang, weights=w[index], k=1) # 相当于l_ist表里存的是各个选项的位置
- for i in l_ist:
- i.click() # 找到标签并点击
- time.sleep(random.randint(0, 1))
- time.sleep(random.randint(0, 1))
-
- def renzheng(driver):
- # 智能验证,找到智能认证的标签
- bth = driver.find_element_by_css_selector('#layui-layer1 > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0')
- bth.click()#点击
- time.sleep(4)
- rectBottom = driver.find_element_by_css_selector('#rectBottom') #提交按钮
- rectBottom.click() #点击
- time.sleep(5)
-
- def huakuai():
- # 当次数多了的时候就会出现滑块,这里是模拟人手解决滑块拖动
- pyautogui.moveTo(random.randint(494, 496), 791, 0.2)#控制鼠标移动到x,y处,耗时0.2秒
- time.sleep(1)
- pyautogui.dragTo(random.randint(888, 890), 791, 1)#让鼠标点击并拖拽到x,y处,耗时1秒
- time.sleep(1)
- pyautogui.click(random.randint(652, 667), random.randint(793, 795))#让鼠标点击x,y处
- time.sleep(1)
- pyautogui.moveTo(random.randint(494, 496), 791, 0.2)#控制鼠标移动到x,y处,耗时0.2秒
- time.sleep(1)
- pyautogui.dragTo(random.randint(888, 890), 791, 1)#让鼠标点击并拖拽到x,y处,耗时1秒
-
- def zonghe(times):
- for i in range(0, times):
- # 初始配置,地址
- url_survey = 'https://www.wjx.cn/vm/ODAvYPc.aspx '
- option = webdriver.ChromeOptions()
- option.add_experimental_option('excludeSwitches', ['enable-automation'])
- option.add_experimental_option('useAutomationExtension', False)
- # 本地下载的谷歌浏览器地址
- option.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
- # 下载好的Chrome驱动的地址
- s = Service("chromedriver_win32/chromedriver.exe")
- driver = webdriver.Chrome(service=s)
- driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
- {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
- with open("明星.txt", "r", encoding="utf-8") as f:
- an_swer = f.readlines()
- an_swer = [line.strip("\n") for line in an_swer]
- answer={}
- for index, value in enumerate(an_swer):
- answer[index]=value
- index=[i for i in range(0,42)]
- # 启动要填写的地址
- driver.get(url_survey)
- tiankong(driver, [1,2,3],index,answer) # 调用填空题函数
- jvzheng(driver, [1,2,3])
- danxuan(driver, [3, 4, 5, 8, 9]) # 调用单选函数#[]里是位置标志数字
- try:
- liangbiao(driver,[6])
- duoxuan(driver,[7]) # 调用多选函数
- gundong(driver, 600) # 调用滚动屏幕函数,如果不需要则注释掉
- except:
- print("直接跳到问卷结束提交")
-
- # 最后交卷点击提交
- time.sleep(random.randint(0, 1))
- # driver.find_element_by_xpath('//*[@id="divContent"]').click()
- # 找到提交的css并点击
- # button=driver.find_element_by_css_selector('#ctlNext')
- # webdriver.ActionChains(driver).move_to_element(button).click(button).perform()
- try:
- driver.find_element_by_css_selector('#ctlNext').click()
- except:
- print("无法提交")
-
- time.sleep(4)
- try:
- renzheng(driver) # 智能认证函数调用
- huakuai() # 滑块函数调用
- except:
- print("未出现验证")
- print('已经提交了{}次问卷'.format(int(i) + int(1)))
- time.sleep(4)
- driver.quit()#停止
-
- if __name__ == "__main__":
- zonghe(170)#里面填写的数是表示要提交多少次问卷
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。