python2 爬虫 爬西农教务管理系统查成绩

这是python2的。
用到了pytesseract来做验证码识别自动填验证码,不过识别率不是很高,识别一些简单的验证码还好。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- 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',"2014012528"),
('j_password',"3976966"),
('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")

下一篇我发python3的,不过没有了验证码识别,我电脑之前装的是python3.6 貌似还不支持pytesseract

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器