- 前言
- 一、前期准备
- 二、具体步骤
- 1.导入库与预定义变量
- 2.打开浏览器,防止识别为脚本
- 3浏览问题,选择问题
- 4提交按钮,验证
- 全部代码
前言
问卷星已经成为收集问卷的一个很重要的工具,有时可以用来报名参加活动,有时可以用来收集某些领域相关的情况,为了更快地填写问卷,特别是报名参加一些名额有限的活动的时候,或者批量填写问卷,可以利用python写个问卷星自助填写程序。
提示:以下是本篇文章正文内容,下面案例可供参考,码字不易,觉得有用的可以点赞收藏关注。
一、前期准备我采用的是python的selenium模块,前期准备工作需要下载chrome驱动,然后要有python ide(如pycharm)
谷歌浏览器驱动链接
找到自己的谷歌浏览器版本下载就可以啦。谷歌浏览器的查看方式为:打开浏览器->…->设置->关于Chrome:
下载后,解压,打开放在Python的目录下面。
导入库,没有的就用pip自定义安装,预定义变量可以方便修改
from selenium import webdriver from selenium.webdriver.common.by import By import time import random #填写问卷星问卷的地址,2021年都是带vm字符偏多 urlip = "https://www.wjx.top/vm/xxxx.aspx" #名字 name = "小红" #性别 sex=2 #手机 phone = '18100000000' #邮箱 Email = 'xiaohong@foxmail.com'2.打开浏览器,防止识别为脚本
这里参考的是
https://blog.csdn.net/qq_45717425/article/details/119737648
# 给出所需的url和option参数 url_survey = (urlip) # 根据需要填写url #防止被浏览器识别为脚本 option = webdriver.ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=option) driver.execute_cdp_cmd('Page.addscriptToevaluateOnNewdocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'}) driver.get(url_survey) time.sleep(0.2)
3浏览问题,选择问题
1、首先找到每个问题,answers = driver.find_elements(By.CSS_SELECtOR, “.ui-field-contain”)
用answers存储每个问题,然后可以遍历问题,用i存储每个问题的序号
2、如何解析问题呢, 可以用title = answer.find_element(By.CSS_SELECTOR, “.field-label”),
找到问题的标题,然后判断其中的问题是什么?
3、接着就找到选项,进行解析、选择,如果是填空类的,如姓名,可以使用send_keys
if (“姓名” in title.text or “名字” in title.text):
idfind=“q%d”%i
a = driver.find_element_by_id(idfind)
a.send_keys(name)
如果是选项类,如性别,要看按钮是什么类型,可以对每个选项进行解析
例如:
radios = answer.find_elements(By.CSS_SELECTOR, “.ui-radio”)
可以存储每个按钮,然后遍历,找到想要的按钮,然后用click()函数去点击
for radio in radios:
j += 1
if (sex in (radio.text)):
radiofind = “//*[@id=‘div%d’]/div[2]/div[%d]/div” % (i, j)(这里是用xpath格式去找到按钮在哪)
print(radiofind)
driver.find_element_by_xpath(radiofind).click()(这里可以点击按钮)
注意,用css_selector去找对应框时,要用F12开发者选项去看具体的是什么类型,才能确保找对,填空类可能有不同类型,如textarea。
answers = driver.find_elements(By.CSS_SELECTOR, ".ui-field-contain") i = 0 for answer in answers: try: i+=1 driver.execute_script("arguments[0].scrollIntoView();", answer) title = answer.find_element(By.CSS_SELECTOR, ".field-label") print(title.text) ### 有选项 if ("姓名" in title.text or "名字" in title.text): idfind="q%d"%i a = driver.find_element_by_id(idfind) a.send_keys(name) elif ("学号" in title.text ): idfind = "q%d" % i driver.find_element_by_id(idfind).send_keys(cardnumber) elif ("手机" in title.text or "电话" in title.text): idfind = "q%d" % i driver.find_element_by_id(idfind).send_keys(phone) elif ("性别" in title.text): idfind = "//*[@id='div%d']/div[2]/div[1]/div" % i print(idfind) driver.find_element_by_xpath(idfind).click() radios = answer.find_elements(By.CSS_SELECTOR, ".ui-radio") j = 0 for radio in radios: j += 1 if (sex in (radio.text)): radiofind = "//*[@id='div%d']/div[2]/div[%d]/div" % (i, j) print(radiofind) driver.find_element_by_xpath(radiofind).click() elif ("邮箱" in title.text or "mail" in title.text): idfind = "q%d" % i driver.find_element_by_id(idfind).send_keys(email) elif ("时间" in title.text ): radios = answer.find_elements(By.CSS_SELECTOR, ".ui-radio") j = 0 for radio in radios: print(radio.text) j += 1 radiofind = "//*[@id='div%d']/div[2]/div[%d]/div" % (i, j) print(radiofind) driver.find_element_by_xpath(radiofind).click() checks = answer.find_elements(By.CSS_SELECTOR, ".ui-checkbox") j = 0 for radio in checks: print(radio.text) j += 1 radiofind = "//*[@id='div%d']/div[2]/div[%d]/div" % (i, j) print(radiofind) driver.find_element_by_xpath(radiofind).click() else: ans = answer.find_elements(By.CSS_SELECTOR, ".ui-controlgroup") if ans: print("ans:", ans) lsans = random.choice(ans) lsans.click() continue inputtext = answer.find_element_by_css_selector("textarea") inputtext.send_keys('没有') except Exception as e: print(e)
4提交按钮,验证
找到提交按钮,提交,可能需要验证,最后如果是想出错了自己填写,可以加sleep函数,给自己时间填写,如果是批量填写,想要快速就不用。
time.sleep(1) am = driver.find_element_by_xpath("//*[@id='ctlNext']") am.click() time.sleep(0.5) # 模拟点击智能验证按钮 # 先点确认 driver.find_element_by_xpath("//button[text()='确认']").click() # 再点智能验证提示框,进行智能验证 driver.find_element_by_xpath("//div[@id='captcha']").click() print("end") time.sleep(100)
全部代码
from selenium import webdriver from selenium.webdriver.common.by import By import time import random #填写问卷星问卷的地址 urlip = "https://www.wjx.top/vm/trZjDys.aspx" #名字 name = "小红" #学号 cardnumber="202130072021" #性别 sex="女" #手机 phone = '18100000000' #邮箱 email = 'xiaohong@foxmail.com' def auto(): print("正在执行 *** 作......") # 给出所需的url和option参数 url_survey = (urlip) # 根据需要填写url #防止被浏览器识别为脚本 option = webdriver.ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=option) driver.execute_cdp_cmd('Page.addscriptToevaluateOnNewdocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'}) driver.get(url_survey) time.sleep(0.2) answers = driver.find_elements(By.CSS_SELECTOR, ".ui-field-contain") i = 0 for answer in answers: try: i+=1 driver.execute_script("arguments[0].scrollIntoView();", answer) title = answer.find_element(By.CSS_SELECTOR, ".field-label") print(title.text) ### 有选项 if ("姓名" in title.text or "名字" in title.text): idfind="q%d"%i a = driver.find_element_by_id(idfind) a.send_keys(name) elif ("学号" in title.text ): idfind = "q%d" % i driver.find_element_by_id(idfind).send_keys(cardnumber) elif ("手机" in title.text or "电话" in title.text): idfind = "q%d" % i driver.find_element_by_id(idfind).send_keys(phone) elif ("性别" in title.text): idfind = "//*[@id='div%d']/div[2]/div[1]/div" % i print(idfind) driver.find_element_by_xpath(idfind).click() radios = answer.find_elements(By.CSS_SELECTOR, ".ui-radio") j = 0 for radio in radios: j += 1 if (sex in (radio.text)): radiofind = "//*[@id='div%d']/div[2]/div[%d]/div" % (i, j) print(radiofind) driver.find_element_by_xpath(radiofind).click() elif ("邮箱" in title.text or "mail" in title.text): idfind = "q%d" % i driver.find_element_by_id(idfind).send_keys(email) elif ("时间" in title.text ): radios = answer.find_elements(By.CSS_SELECTOR, ".ui-radio") j = 0 for radio in radios: print(radio.text) j += 1 radiofind = "//*[@id='div%d']/div[2]/div[%d]/div" % (i, j) print(radiofind) driver.find_element_by_xpath(radiofind).click() checks = answer.find_elements(By.CSS_SELECTOR, ".ui-checkbox") j = 0 for radio in checks: print(radio.text) j += 1 radiofind = "//*[@id='div%d']/div[2]/div[%d]/div" % (i, j) print(radiofind) driver.find_element_by_xpath(radiofind).click() else: ans = answer.find_elements(By.CSS_SELECTOR, ".ui-controlgroup") if ans: print("ans:", ans) lsans = random.choice(ans) lsans.click() continue inputtext = answer.find_element_by_css_selector("textarea") inputtext.send_keys('没有') except Exception as e: print(e) time.sleep(1) am = driver.find_element_by_xpath("//*[@id='ctlNext']") am.click() time.sleep(0.5) # 模拟点击智能验证按钮 # 先点确认 driver.find_element_by_xpath("//button[text()='确认']").click() # 再点智能验证提示框,进行智能验证 driver.find_element_by_xpath("//div[@id='captcha']").click() print("end") time.sleep(100) if __name__ == '__main__': auto()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)