python2 爬虫爬教务管理,还有验证码识别

2017-01-14 08:42:45来源:CSDN作者:qq_25560423人点击

# -*- coding: utf-8 -*-# *********************************#       vresion:python 2#         author:张琼#       西北农林科技大学 计算机141# *********************************#from urllib import request,parseimport cookielibimport urllibimport urllib2import reimport iofrom PIL import Imageimport pytesseract#模拟登录测试模块print('loging info my ssfw')cookie=cookielib.CookieJar()#储存获取到的cookieopener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))CaptchaUrl = "http://jwgl.nwsuaf.edu.cn/academic/getCaptcha.do" #验证码图片URLpicture = opener.open(CaptchaUrl).read()local = open('e:/image1.jpg', 'wb')local.write(picture) #验证码图片保存下来local.close()img=Image.open('e:/image1.jpg')vcode = pytesseract.image_to_string(img)#验证码识别print(vcode)#SecretCode = input('输入验证码: ') #自己查看图片然后输入SecretCode=vcodeprint(SecretCode)loging_data=urllib.urlencode([    ('j_username',"20140xxxxxx"),    ('j_password',"xxxxxx"),    ('j_captcha',SecretCode)])#POST用到的数据#请求头headers = {    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '+    '(KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36'}#构造requestreq=urllib2.Request(url='http://jwgl.nwsuaf.edu.cn/academic/j_acegi_security_check',                    data=loging_data.encode(encoding='utf-8'),                    headers=headers)try:    result=opener.open(req)#访问请求的链接    # print(result.read().decode('utf-8'))except urllib2.HTTPError:    print("connect failed")try:    result=opener.open('http://jwgl.nwsuaf.edu.cn/academic/manager/score/studentOwnScore.do?groupId=&moduleId=2021')#进入教务系统个人成绩信息界面    print(result.read().decode('utf-8'))except urllib2.HTTPError:    print("error")

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台