• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
玅神
博客园    首页    新随笔    联系   管理    订阅  订阅

为什么我还可以继续使用python自动填问卷星?

1.由于问卷星系统的升级,利用selenium已经无法实现自动填写问卷.

 

 

 

 

 附(selenium源码):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random
def run():
    #1 问卷填报日期
    driver.find_element_by_id('q1').click()
    driver.switch_to.frame('__calendarIframe')
    time.sleep(1)
    driver.find_element_by_id('selectTodayButton').click()
    driver.switch_to.default_content()
    #2 体温测量日期
    driver.find_element_by_id('q2').click()
    driver.switch_to.frame('__calendarIframe')
    time.sleep(1)
    driver.find_element_by_id('selectTodayButton').click()
    driver.switch_to.default_content()
    #3 班级
    js = "document.getElementById(\"q3_1\").click()"
    driver.execute_script(js)
    #4 学号
    driver.find_element_by_id('q4').send_keys("20183769")
    #5 姓名
    driver.find_element_by_id('q5').send_keys("赵常恒")
    #6 性别
    js="document.getElementById(\"q6_1\").click()"
    driver.execute_script(js)
    #7 所在地区
    driver.find_element_by_xpath('//*[@id="divquestion7"]/ul/li[2]/a').click()
    #8 核酸
    driver.find_element_by_xpath('//*[@id="divquestion8"]/ul/li[1]/a').click()
    driver.find_element_by_xpath('//*[@id="divquestion8"]/ul/li[1]/input[2]').send_keys("1月7日阴性")
    #9 隔离情况
    js="document.getElementById(\"q9_2\").click()"
    driver.execute_script(js)
    #10 密切接触
    js = "document.getElementById(\"q10_2\").click()"
    driver.execute_script(js)
    #12 病例
    js = "document.getElementById(\"q12_2\").click()"
    driver.execute_script(js)
    time.sleep(1)
    #14 体温
    num=random.randint(0,6)
    driver.find_element_by_id('q14').send_keys("36."+str(num)+"/"+"36."+str(num))
    time.sleep(1)
    #15 同住人员密切接触
    js = "document.getElementById(\"q15_2\").click()"
    driver.execute_script(js)
    time.sleep(1)
    #17 同住人员病例
    js = "document.getElementById(\"q17_2\").click()"
    driver.execute_script(js)
    time.sleep(1)
    #19 地理位置
    driver.find_element_by_id('q19').click()
    time.sleep(5)
    driver.switch_to.frame("PDF_i_chezchenz")
    time.sleep(1)
    # 1
    driver.find_element_by_id('txtInput').send_keys('河北省邢台市广宗县牛家寨村')
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="divauto"]/ul/li[1]').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="divCurLoc"]/a').click()
    driver.switch_to.default_content()
    time.sleep(1)
    # 2
    #driver.find_element_by_xpath('//*[@id="mapcontainer"]/div[3]/div/div').click()
    #time.sleep(1)
    #driver.find_element_by_xpath('//*[@id="divCurLoc"]/a').click()
    #driver.switch_to.default_content()
    #20 居住地变化
    js="document.getElementById(\"q20_2\").click()"
    driver.execute_script(js)
    time.sleep(1)
    #22 异常情况
    driver.find_element_by_id('q22').send_keys('无')
    time.sleep(1)
    #23 同住人员
    driver.find_element_by_id('select2-q23-container').click()
    driver.find_element_by_xpath('/html/body/span/span/span[1]/input').send_keys('0')
    driver.find_element_by_xpath('/html/body/span/span/span[1]/input').send_keys(Keys.ENTER)
    time.sleep(10)
    #提交

if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get("***")
    run()
    # 提交
selenium源码

 2.利用pyppeteer实现自动填写问卷操作

 附(pyppeteer源码):

import asyncio
from pyppeteer import launch
from pyppeteer_stealth import stealth #反爬
import random
import time
async def run():
    driver = await launch({
        # 谷歌浏览器的安装路径
        'executablePath': 'C:\Program Files\Google\Chrome\Application\chrome.exe',
        # Pyppeteer 默认使用的是无头浏览器
        'headless': False,
        # 设置Windows-size和Viewport大小来实现网页完整显示
        'args': ['--no-sandbox', '--window-size=1024,768']
    })
    page = await driver.newPage()
    await page.setViewport({'width': 1024, 'height': 768})
    # 反爬虫跳入网页
    await stealth(page)
    await page.goto('***')
    time.sleep(1)
    #1 问卷填报日期
    date=await page.querySelector('#q1')
    await date.click()
    frame =page.frames
    date2= await frame[1].querySelector('#selectTodayButton')
    await date2.click()
    #2 体温测量日期
    date = await page.querySelector('#q2')
    await date.click()
    frame = page.frames
    date2 = await frame[1].querySelector('#selectTodayButton')
    await date2.click()
    #3 班级
    await page.click('#divquestion3 > ul > li > a')
    #4 学号
    await page.type('#q4','20183769')
    #5 姓名
    await page.type('#q5','赵常恒')
    # 6 性别
    await page.click('#divquestion6 > ul:nth-child(2) > li:nth-child(1) > a')
    #7 所在地区
    await page.click('#divquestion7 > ul > li:nth-child(2) > a')
    #8 核酸
    await page.click('#divquestion8 > ul > li:nth-child(1) > a')
    await page.type('#divquestion8 > ul > li:nth-child(1) > input.underline','第一次1月7日阴性,第二次1月11日阴性')
    #9 隔离情况
    await page.click('#divquestion9 > ul > li:nth-child(2) > a')
    #10 密切接触
    await page.click('#divquestion10 > ul:nth-child(2) > li:nth-child(2) > a')
    #12 病例
    await page.click('#divquestion12 > ul:nth-child(2) > li:nth-child(2) > a')
    #14 体温
    temperature="36."+str(random.randint(2,6))+'/'+"36."+str(random.randint(2,6))
    print(temperature)
    await page.type('#q14',temperature)
    #15 同住人员密切接触
    await page.click('#divquestion15 > ul:nth-child(2) > li:nth-child(2) > a')
    #17 同住人员病例
    await page.click('#divquestion17 > ul:nth-child(2) > li:nth-child(2) > a')
    #19 地理位置
    address= await page.querySelector('#q19')
    await address.click()
    time.sleep(5)
    frame=page.frames
    await frame[2].type('#txtInput','河北省邢台市广宗县牛家寨村')
    time.sleep(1)
    await frame[2].click('#btnSearch')
    time.sleep(1)
    await frame[2].click('#divauto > ul > li:nth-child(1) > div')
    time.sleep(1)
    await frame[2].click('.ensure_btn')
    #20 居住地变化
    await page.click('#divquestion20 > ul:nth-child(2) > li:nth-child(2) > a')
    # 22 异常情况
    await page.type('#q22','无')
    #23 同住人员
    await page.click('#select2-q23-container')
    persion=await page.xpath('/html/body/span/span/span[2]/ul/li[2]')
    await persion[0].click()
    time.sleep(2)
    #提交
    await page.click('#submit_button')
if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(run())
pyppeteer源码

 

posted @ 2021-01-12 14:15  玅神  阅读(194)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3