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

# -*- coding: utf-8 -*-
# *********************************
#       vresion:python 2
#         author:张琼
#       西北农林科技大学 计算机141
# *********************************
#from urllib import request,parse
import cookielib
import urllib
import urllib2
import re
import io
from PIL import Image
import pytesseract
#模拟登录测试模块
print('loging info my ssfw')
cookie=cookielib.CookieJar()#储存获取到的cookie
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
CaptchaUrl = "http://jwgl.nwsuaf.edu.cn/academic/getCaptcha.do" #验证码图片URL
picture = 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=vcode
print(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'
}

#构造request
req=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")
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值