Python模拟登录验证码(代码简单)


Posted in Python onFebruary 06, 2016

废话不多说了,直接给大家贴代码了。

import urllib
import urllib2
import cookielib
def getImg(picurl):
'''
request for random_code picture and cookie 
'''
pic = opener.open(picurl).read()
with open('./verifyImg.jpg','wb') as emptyPic:
emptyPic.write(pic)
def verifyImg(picpath):
'''
TODO
'''
pass
def login(coo):
loginUrl = 'http://jwxt.wust.edu.cn/whkjdx/Logon.do?method=logon'
data = {'USERNAME':'*****', 'PASSWORD':'*****', 'RANDOMCODE':coo}
#encode the postData
postData = urllib.urlencode(data)

user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = {'User-Agent':user_agent,'Referer':'http://jwxt.wust.edu.cn/whkjdx/framework/main.jsp'}
#generate a Request with url,postData headers and cookie
request = urllib2.Request(loginUrl, postData, headers = header)
#post data
content = opener.open(request)
#get html file
mainUrl = 'http://jwxt.wust.edu.cn/whkjdx/framework/main.jsp'
mainContent = opener.open(mainUrl).read()
print mainContent 
if __name__ == '__main__':
cookie = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
picurl = 'http://jwxt.wust.edu.cn/whkjdx/verifycode.servlet'
getImg(picurl)
#verifyImg(picpath) 
randomCode = raw_input('Plz input teh randomcode:')
login(randomCode)

以上代码大家看起来很容易吧,有不明白的地方欢迎留言,我会在第一时间和大家取得联系的。在此三水点靠木小编祝大家新年快乐。

Python 相关文章推荐
python 域名分析工具实现代码
Jul 15 Python
Python写的Socks5协议代理服务器
Aug 06 Python
Python的Flask开发框架简单上手笔记
Nov 16 Python
Python之多线程爬虫抓取网页图片的示例代码
Jan 10 Python
python 实现对文件夹中的图像连续重命名方法
Oct 25 Python
python之验证码生成(gvcode与captcha)
Jan 02 Python
Python pandas.DataFrame调整列顺序及修改index名的方法
Jun 21 Python
python交易记录整合交易类详解
Jul 03 Python
使用Python和OpenCV检测图像中的物体并将物体裁剪下来
Oct 30 Python
浅析Django 接收所有文件,前端展示文件(包括视频,文件,图片)ajax请求
Mar 09 Python
Python Flask框架实现简单加法工具过程解析
Jun 03 Python
python使用ctypes库调用DLL动态链接库
Oct 22 Python
Python上传package到Pypi(代码简单)
Feb 06 #Python
深入讲解Java编程中类的生命周期
Feb 05 #Python
python&MongoDB爬取图书馆借阅记录
Feb 05 #Python
Python中常用操作字符串的函数与方法总结
Feb 04 #Python
Python中的字符串类型基本知识学习教程
Feb 04 #Python
Python的math模块中的常用数学函数整理
Feb 04 #Python
详解Python编程中基本的数学计算使用
Feb 04 #Python
You might like
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
2013/07/03 PHP
php三维数组去重(示例代码)
2013/11/26 PHP
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
2013/11/26 PHP
PHP会话操作之cookie用法分析
2016/09/28 PHP
PHP创建单例后台进程的方法示例
2017/05/23 PHP
Laravel中前端js上传图片到七牛云的示例代码
2017/09/04 PHP
PHP中用Trait封装单例模式的实现
2019/12/18 PHP
微信JS接口汇总及使用详解
2015/01/09 Javascript
JavaScript通过prototype给对象定义属性用法实例
2015/03/23 Javascript
20分钟轻松创建自己的Bootstrap站点
2016/05/12 Javascript
老生常谈JavaScript数组的用法
2016/06/10 Javascript
jQuery中的on与bind绑定事件区别实例详解
2017/02/28 Javascript
vue-music关于Player播放器组件详解
2017/11/28 Javascript
响应式框架Bootstrap栅格系统的实例
2017/12/19 Javascript
Bootstrap popover 实现鼠标移入移除显示隐藏功能方法
2018/01/24 Javascript
vue.js中$set与数组更新方法
2018/03/08 Javascript
Python交换变量
2008/09/06 Python
解决python线程卡死的问题
2019/02/18 Python
详解python列表生成式和列表生成式器区别
2019/03/27 Python
基于Python生成个性二维码过程详解
2020/03/05 Python
Python直接赋值及深浅拷贝原理详解
2020/09/05 Python
python分布式爬虫中消息队列知识点详解
2020/11/26 Python
python爬虫多次请求超时的几种重试方法(6种)
2020/12/01 Python
卡塔尔航空官方网站:Qatar Airways
2017/02/08 全球购物
历史学专业个人的自我评价
2013/10/13 职场文书
行政总经理岗位职责
2013/12/05 职场文书
自主招生自荐信指南
2014/02/04 职场文书
中学校庆方案
2014/03/17 职场文书
文艺晚会主持词
2014/03/24 职场文书
五一口号
2014/06/19 职场文书
职业生涯规划书怎么写?
2014/09/14 职场文书
2014年档案管理工作总结
2014/11/17 职场文书
一年级小学生评语大全
2014/12/25 职场文书
2016年暑期教师培训心得体会
2016/01/09 职场文书
SpringBoot整合阿里云视频点播的过程详解
2021/12/06 Java/Android
redis复制有可能碰到的问题汇总
2022/04/03 Redis