• 通过Selenium批量填写问卷


    起因

    为啥要做这个呢。因为某天老婆让我帮她填问卷,而且是填几十份一模一样的,点的我鼠标都要冒烟了。所以研究下自动化工具,咱肯定不能一个个手工去点呀。

    实现过程

    1. 用edge打开浏览器

      driver = webdriver.Edge(options=self.option)
      # 随机User-agent
      num = random.randint(0, 2)
      driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": self.ua[num]})
      # 将webdriver属性置为undefined
      driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                             {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
      
      driver.get(self.wj_url)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    2. 获取页面上的所有题目,进行遍历

      questions = driver.find_elements(By.CLASS_NAME, "field.ui-field-contain")
      for i in range(1, len(questions) + 1):
          xpath = '//*[@id="div{}"]'.format(i)
          question = driver.find_element(By.XPATH, xpath)
          # 获取题目类型
          ques_type = question.get_attribute("type")
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. 根据题目类型对题目进行处理,读取设定好的选项,进行填写。以单选题为例

      def single_choice(driver, id, prob, idx):
          xpath = '//*[@id="div{}"]/div[2]/div'.format(id)
          answers = driver.find_elements(By.XPATH, xpath)
          # 如果没有传入比例,默认为等比例
          p = preprocess_prob(prob.get(idx), len(answers))
          choice = numpy.random.choice(a=numpy.arange(1, len(answers) + 1), p=p)
          xpath = '//*[@id="div{}"]/div[2]/div[{}]'.format(id, choice)
          # driver.find_element(By.XPATH, xpath).click()
          ActionChains(driver).move_to_element(driver.find_element(By.XPATH, xpath)).click().perform()
          idx += 1
          return idx
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

      其中选项的比例是由如下代码计算的

      def preprocess_prob(prob, length):
          """计算该选项的比例"""
          if len(prob) == 0:
              prob = numpy.ones(length)
          return [i/sum(prob) for i in prob]
      
      • 1
      • 2
      • 3
      • 4
      • 5

    完整代码链接:https://github.com/h-kayotin/kayotin_sel

    如何使用

    请安装所需的库:requests、numpy、Selenium

    1. 在config.py中设定问卷答案:

      # 设置每个问题的填写情况,按照每个选项的概率填写。例如ABCD对应1111的话,就是各25%
      # !!! 注意:选项长度一定要和实际一样,否则会报错
      answers = {
          1: [1, 0],
          2: [1, 0, 0, 1],
          3: [1, 1, 0, 0, 0],
      }
      
      # 简答题,需要设置好文本;如没有请无视即可
      answer_list = {
          6: ["文本1", "文本2"]
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12

      以上的问题代表了这个问卷的填写情况:问卷链接

    2. 在config.py中填写代理IP(选填:如果不使用代理IP,这个api里保持如下就行,但不能为空

      # 实际上不使用代理也可以,无非就是做的多了会有验证
      api = "https://service.ipzan.com/core-extract"
      
      • 1
      • 2
    3. 运行主程序auto_wjx.py,选择做多少份

          my_url = "https://www.wjx.cn/vm/rJoCZrn.aspx"
          my_wjx = MyWjx(my_url, 10)
          my_wjx.fill_in()  # 做1份
      	  my_wjx.do_works()  # 做10份
      
      • 1
      • 2
      • 3
      • 4

    参考链接:

    该工具很大程度上参考了如下链接:

    https://github.com/zzmvp-1/wjx-auto-fill

  • 相关阅读:
    Cadence OrCAD Could not check the CaptureCIS feature licence
    用DIV+CSS技术设计的西安旅游网站18页(web前端网页制作课作业)HTML+CSS旅游网站设计与实现
    数据结构实验六 栈和队列的基本操作及应用
    mysql explain和DESC性能分析
    《海洋湍流导论》-Thorpe-2007读书笔记-第四章
    jmeter如何压测和存储
    封装withRouter并且使用
    MySQL 练习<2>
    【PyTorch][chapter 20][李宏毅深度学习]【无监督学习][ GAN]【实战】
    使用堆栈处理字符串
  • 原文地址:https://blog.csdn.net/kayotin/article/details/136369769