写此文的目的是为了方便寒假自己忘记填问卷星
一开始的想法和去年一样,去年就写过一版,想着今年不过就是改改数据,换换id而已,另外没想到的事情发生了。。。
满怀信心的写完代码
from selenium import webdriverimport timeimport randomdef run():browser = webdriver.Chrome()browser.get(\"网址\")# 1js = \"document.getElementById(\\\"q1\\\").click()\"browser.execute_script(js)browser.switch_to.frame(\"__calendarIframe\")js2 = \"document.getElementById(\\\"selectTodayButton\\\").click()\"browser.execute_script(js2)browser.switch_to.parent_frame()# 2js = \"document.getElementById(\\\"q2\\\").click()\"browser.execute_script(js)browser.switch_to.frame(\"__calendarIframe\")js2 = \"document.getElementById(\\\"selectTodayButton\\\").click()\"browser.execute_script(js2)browser.switch_to.parent_frame()# 3js = \"document.getElementById(\\\"q3_1\\\").click()\"browser.execute_script(js)# 4browser.find_element_by_id(\"q4\").send_keys(\"学号\")# 5browser.find_element_by_id(\"q5\").send_keys(\"姓名\")# 6js = \"document.getElementById(\\\"q6_1\\\").click()\"browser.execute_script(js)# 7js = \"document.getElementById(\\\"q7_3\\\").click()\"browser.execute_script(js)# 8js = \"document.getElementById(\\\"q8_1\\\").click()\"browser.execute_script(js)browser.find_element_by_class_name(\"underline\").send_keys(\"第一次1月9日阴性\")# 9js = \"document.getElementById(\\\"q9_2\\\").click()\"browser.execute_script(js)# 10js = \"document.getElementById(\\\"q10_2\\\").click()\"browser.execute_script(js)# 11js = \"document.getElementById(\\\"q12_2\\\").click()\"browser.execute_script(js)# 12browser.find_element_by_id(\"q14\").send_keys(\"36.\"+str(random.randint(0,5))+\"/36.\"+str(random.randint(0,5)))#13js = \"document.getElementById(\\\"q15_2\\\").click()\"browser.execute_script(js)#14js = \"document.getElementById(\\\"q17_2\\\").click()\"browser.execute_script(js)#15browser.find_element_by_id(\"q19\").click()time.sleep(1)browser.switch_to.frame(\"PDF_i_chezchenz\")browser.find_element_by_id(\"txtInput\").send_keys(\"地址\")js3=\"document.getElementById(\\\"btnSearch\\\").click()\"browser.execute_script(js3)time.sleep(1)browser.find_element_by_css_selector(\"a.ensure_btn\").click()time.sleep(1)browser.switch_to.default_content()# 16js = \"document.getElementById(\\\"q20_2\\\").click()\"browser.execute_script(js)# 17browser.find_element_by_id(\"q22\").send_keys(\"无\")# 18browser.find_element_by_id(\'select2-q23-container\').click()# js = \"document.getElementById(\'select2-q23-container\').click()\"# browser.execute_script(js)browser.find_element_by_xpath(\"//li[@class=\'select2-results__option\'][1]\").click()# js = \"document.getElementById(\\\"select2-q23-result-ei9q-1\\\").click()\"# browser.execute_script(js)# submitjs = \"document.getElementById(\\\"submit_button\\\").click()\"browser.execute_script(js)if __name__ == \"__main__\":while True:run()time.sleep(4)break
数据都填上了,可他来了一个这玩意
然后
之后了解到用用pyppeteer解决反爬的方法,安装python第三方库asyncio、pyppeteer和pyppeteer_stealth
完整代码如下:
import asynciofrom pyppeteer import launchfrom pyppeteer_stealth import stealth #反爬import randomimport timeasync def run():driver = await launch({# 谷歌浏览器的安装路径\'executablePath\': \'C:\\Program Files (x86)\\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.framesdate2= await frame[1].querySelector(\'#selectTodayButton\')await date2.click()#2 体温测量日期date = await page.querySelector(\'#q2\')await date.click()frame = page.framesdate2 = await frame[1].querySelector(\'#selectTodayButton\')await date2.click()#3 班级await page.click(\'#divquestion3 > ul > li > a\')#4 学号await page.type(\'#q4\',\'学号\')#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(3) > a\')#8 核酸await page.click(\'#divquestion8 > ul > li:nth-child(1) > a\')await page.type(\'#divquestion8 > ul > li:15a8nth-child(1) > input.underline\',\'第一次1月9日阴性\')#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.framesawait frame[2].type(\'#txtInput\',\'地址\')time.sleep(1)await frame[2].click(\'#btnSearch\')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(\"//li[@class=\'select2-results__option\'][1]\")await persion[0].click()time.sleep(2)#提交await page.click(\'#submit_button\')if __name__ == \'__main__\':asyncio.get_event_loop().run_until_complete(run())
就提交成功了!!!