Selenium模拟选课
来源:互联网 发布:张亮中餐厅知乎 编辑:程序博客网 时间:2024/05/22 14:34
使用selenium+python实现无人看守选课。
用到的一些模块
- selenium
selenium在前面的文章中曾经介绍过,就是模拟浏览器的一个第三方模块,通过提供的各种方法模拟控制browser,同时提供了无UI的Browserdriver,减少渲染开销,提升运行速度.
- time
本例中实现暂停扫描功能,防止服务器对IP封锁
- re
正则表达模块,获取相关数据
常用的方法
- b=webdriver.PhantomJS()
开启Selenium的一个webdriver实例,这里使用的是无UI的PhantomJS,通过它进行对browser的操作
- b.get(url)
url是我们想要request的网络地址,get方法实际上就是我们在browser中地址栏填入url并回车访问的过程
- find_element_by_xpath
一系列的find_element_by方法,通过各种定位方法(Xpath、css-seletor、id、tagname)拿到需要操纵的元素句柄
- element.send_keys(value)
在input或者其他可输入标签元素中填入keyword,在使用这个方法之前,建议使用element.clear()清楚之前的所有信息,因为send_keys()方法是直接在后面append
- element.get_attribute(attr)
获取元素的属性值,例如href、onclick
- b.page_source
获取当前driver停留页面的源网页代码
- re.findall(pattern,str)
re正则过滤,pattern是模式串,根据正则规则编写的模式串,str是原始串,也就是需要匹配的串
实现
from selenium import webdriverimport timeimport recc=webdriver.PhantomJS()#身份认证,一次运行只需要运行一次cc.get('http://auth.bupt.edu.cn/authserver/login?service=http%3a%2f%2fyjxt.bupt.edu.cn%2fULogin.aspx')uname=cc.find_element_by_xpath('//*[@id="username"]')uname.clear()uname.send_keys('*********')passwd=cc.find_element_by_xpath('//*[@id="password"]')passwd.clear()passwd.send_keys('*********')cc.find_element_by_xpath('//*[@id="casLoginForm"]/input[4]').click()xuankeurl='http://yjxt.bupt.edu.cn/Gstudent/Course/PlanCourseOnlineSel.aspx?EID=9kWb0OKGTBF2KzmBt5QNDZLXYu1Fldi6xwxV6Yb1wPA1TrsnKBRXgg==&UID=2016111552'delaylist=[u'班级已全选满',u'选课未开放',u'选课已结束']cc.get(xuankeurl)wantedcourse=cc.find_element_by_xpath('//*[@id="contentParent_dgData_hykFull_42"]')restr=wantedcourse.get_attribute('onclick')jumpurl=(re.findall("classFull\('\?(.+)','classFull'\);",restr))[0]while True: cc.get(xuankeurl) wantedcourse=cc.find_element_by_xpath('//*[@id="contentParent_dgData_hykFull_42"]') if wantedcourse.text in delaylist: print(wantedcourse.text) time.sleep(5) pass else : cc.get('http://yjxt.bupt.edu.cn/Gstudent/Course/PlanClassSelFull.aspx?'+jumpurl) print(cc.page_source) break;
0 0
- Selenium模拟选课
- selenium之自动选课脚本
- 模拟学生选课系统
- 模拟选课过程
- 【NOIP2013模拟联考6】选课
- Java模拟学生选课系统
- JAVA模拟学生选课系统
- Python 模拟登陆教务选课系统
- 【NOIP2013模拟联考6】选课(select)
- selenium 模拟按键F12
- Selenium login 模拟登录
- Selenium 模拟浏览器
- python+Selenium模拟登录
- Selenium模拟鼠标操作
- selenium模拟登陆
- 选课
- 选课
- 选课
- MFC 字符(串)间的转换
- Git使用之路
- string全排列
- Linux命令 --- 查找筛选grep
- C# 中用 PadLeft、PadRight 补足位数
- Selenium模拟选课
- How to fix exception: Failed to instantiate SLF4J LoggerFactory?
- iOS 网络编程大小端格式
- mysql:GTID复制切换成传统复制
- 校园网一键登录注销
- structs2如何获取web资源
- Kinect2的安装以及关于OpenNI2遇到的问题(OpenNI2显示no devices found)
- iOS开发中AFNetworking中如何设置https请求(使用自建证书)
- eclipse的maven项目部署到tomcat