python+selenium 脚本实现每天自动登记的思路详解


Posted in Python onMarch 11, 2020

最近受疫情影响,学校要求每天必须进行健康登记,而我身处大山深处,身体健康,足不出户,奈何总是容易忘记,遂决定手撸一个自动登记的小程序,仅供学习交流之用,话不多说,直接上代码。
配置:Chrom python3.7 selenium库,webdriver等
基本思路,使用selenium模拟浏览器自动登录,需要解决验证码的提取,嵌套表单的提取,弹窗处理,异常处理。
为了防止大家用此网站测试,搞垮服务器,关键网址我已隐藏。

import selenium.webdriver 
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
driver = selenium.webdriver.Chrome()

url = '你的url'
driver.get(url)
source = driver.find_element_by_xpath('//p[@class="mb-md-5 mb-3 welcome-para"]/a')
#source.get_attribute('href').click()
ur2 = source.get_attribute('href')
driver.get(ur2)#来到登录界面
time.sleep(10)
html = driver.page_source
bs = BeautifulSoup(html,"html.parser")
s = bs.findAll(name='div')[14].text[3:7] #获得验证码
time.sleep(5)
#输入用户名,密码,验证码
driver.find_element_by_name("username").send_keys(你的账号)
driver.find_element_by_name("userpwd").send_keys(你的密码)
driver.find_element_by_name("code").send_keys(s)
driver.find_element_by_name("login").click()
time.sleep(5)
#这里是解决页面跳转问题,用了笨办法
url3='你的url'
driver.get(url3)
time.sleep(5)
driver.switch_to.frame('leftFrame')
html = driver.page_source
bs = BeautifulSoup(html,"html.parser")
url4 = 'https://xsswzx.cdu.edu.cn:81/isp/com_user/'
url5 = bs.findAll('a')[43].get('href')
url6=url4+url5
driver.get(url6)
time.sleep(5)
driver.find_element_by_xpath('//input[@value="【一键登记:无变化】"]').click()
dig_alert = driver.switch_to.alert
dig_alert.accept()
time.sleep(5)
try:
 dig_alert = driver.switch_to.alert
 dig_alert.accept()
except:
 pass
time.sleep(10)
try:
 driver.find_element_by_xpath('//input[@value="退出系统"]').click()
except:
 driver.close()
print("登记成功")

执行此程序就可以实现登记了,但是并没有实现每天自动登记,下面我们结合Windows通过Anaconda定时调用python脚本,实现每天定时自动登记。
首先编写一个bat脚本:
#此处为引用别人的内容,参考链接:
https://zhuanlan.zhihu.com/p/50057040

python+selenium 脚本实现每天自动登记的思路详解
python+selenium 脚本实现每天自动登记的思路详解

配置好bat文件后

python+selenium 脚本实现每天自动登记的思路详解
python+selenium 脚本实现每天自动登记的思路详解

为了使电脑在关机的情况下也能自己开机启动此程序(万一你睡过头了呢,对吧),我们配置如下:

python+selenium 脚本实现每天自动登记的思路详解
python+selenium 脚本实现每天自动登记的思路详解
python+selenium 脚本实现每天自动登记的思路详解

点击确定,至此大功告成。

知识点补充:Python实现自动填写网安早上登记信息

放在这里以后还可以参考!!!

from selenium import webdriver
import time
import schedule

def auto_click():
  var1 = 0
  file = open('1.txt', 'r')
  list1 = []
  for num in file:
    list1.append(num)
  list1 = list(map(int, list1))
  while var1 < len(list1):
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get('http://acm.scu.edu.cn/student/weui/sars2.html?from=singlemessage')
    driver.find_element_by_xpath('//*[@id="number"]').send_keys(list1[var1])
    driver.find_element_by_xpath('//*[@id="showTooltips"]').click()
    time.sleep(5)
    driver.quit()
    var1 += 1
schedule.every().day.at('16:19').do(auto_click)
while True:
  schedule.run_pending()
  time.sleep(1)

到此这篇关于python+selenium 脚本实现每天自动登记的思路详解的文章就介绍到这了,更多相关python selenium 每天自动登记内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python网络编程之文件下载实例分析
May 20 Python
TF-IDF算法解析与Python实现方法详解
Nov 16 Python
机器学习经典算法-logistic回归代码详解
Dec 22 Python
浅谈Scrapy框架普通反爬虫机制的应对策略
Dec 28 Python
Python对多属性的重复数据去重实例
Apr 18 Python
Python对数据进行插值和下采样的方法
Jul 03 Python
Python判断字符串是否为字母或者数字(浮点数)的多种方法
Aug 03 Python
解决python通过cx_Oracle模块连接Oracle乱码的问题
Oct 18 Python
Python考拉兹猜想输出序列代码实践
Jul 05 Python
基于h5py的使用及数据封装代码
Dec 26 Python
python实现的批量分析xml标签中各个类别个数功能示例
Dec 30 Python
Anaconda+vscode+pytorch环境搭建过程详解
May 25 Python
Django+boostrap 美化admin后台的操作
Mar 11 #Python
Windows 下python3.8环境安装教程图文详解
Mar 11 #Python
django admin后管定制-显示字段的实例
Mar 11 #Python
Python 3.8 新功能来一波(大部分人都不知道)
Mar 11 #Python
屏蔽Django admin界面添加按钮的操作
Mar 11 #Python
在Mac中PyCharm配置python Anaconda环境过程图解
Mar 11 #Python
python实现飞机大战项目
Mar 11 #Python
You might like
用PHP实现多级树型菜单
2006/10/09 PHP
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
thinkPHP使用post方式查询时分页失效的解决方法
2015/12/09 PHP
javascript中对对层的控制
2006/12/29 Javascript
javascript读取RSS数据
2007/01/20 Javascript
javascript Prototype 对象扩展
2009/05/15 Javascript
Prototype Object对象 学习
2009/07/12 Javascript
xml文档转换工具,附图表例子(hta)
2010/11/17 Javascript
JavaScript学习笔记记录我的旅程
2012/05/23 Javascript
js操作textarea 常用方法总结
2012/12/03 Javascript
jquery属性过滤选择器使用示例
2013/06/18 Javascript
JS组件Bootstrap Table表格行拖拽效果实现代码
2020/08/27 Javascript
使用Script元素发送JSONP请求的方法
2016/06/12 Javascript
在 Vue 项目中引入 tinymce 富文本编辑器的完整代码
2018/05/04 Javascript
Angular动态绑定样式及改变UI框架样式的方法小结
2018/09/03 Javascript
vue 进阶之实现父子组件间的传值
2019/04/26 Javascript
Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
2019/04/26 Javascript
JS实现页面跳转与刷新的方法汇总
2019/08/30 Javascript
[03:06]3分钟带你回顾DOTA2完美盛典&完美大师赛
2017/12/06 DOTA
python 布尔操作实现代码
2013/03/23 Python
将Python的Django框架与认证系统整合的方法
2015/07/24 Python
python 基础教程之Map使用方法
2017/01/17 Python
在Python中使用defaultdict初始化字典以及应用方法
2018/10/31 Python
Python提取频域特征知识点浅析
2019/03/04 Python
Python 正则表达式爬虫使用案例解析
2019/09/23 Python
django框架使用views.py的函数对表进行增删改查内容操作详解【models.py中表的创建、views.py中函数的使用,基于对象的跨表查询】
2019/12/12 Python
利用python下载scihub成文献为PDF操作
2020/07/09 Python
Python hashlib和hmac模块使用方法解析
2020/12/08 Python
用CSS3的box-reflect来制作倒影效果
2016/11/15 HTML / CSS
HTML5的download属性详细介绍和使用实例
2014/04/23 HTML / CSS
会计实习生自我鉴定
2013/12/12 职场文书
广告传媒专业应届生求职信
2014/03/01 职场文书
解除劳动合同协议书
2014/04/14 职场文书
党员干部一句话承诺
2014/05/30 职场文书
社区敬老月活动总结
2015/05/07 职场文书
2016年党建工作简报
2015/11/26 职场文书