首页 > 技术文章 > python3 正方教务系统 爬取数据

Lynwood 2017-08-28 23:47 原文

  1 import requests
  2 import re
  3 import os
  4 from yzm import YDMHttp
  5 class Edu():
  6     def yzm_csh(self):
  7         # 用户名
  8         username = ''
  9         # 密码
 10         password = ''
 11         # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
 12         appid = 3912
 13         # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
 14         appkey = 'e2bafb5103206d8480ddb7ce3af2d082'
 15         # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
 16         self.codetype = 1004
 17         # 超时时间,秒
 18         self.timeout = 60
 19         # 初始化
 20         self.yundama = YDMHttp(username, password, appid, appkey)
 21         # 登陆云打码
 22         uid = self.yundama.login()
 23         print('uid: %s' % uid)
 24         # 查询余额
 25         balance =  self.yundama.balance()
 26         print('balance: %s' % balance)
 27     def get_cpic_res(self,pic_name):
 28         # 图片文件
 29         filename = pic_name
 30         # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
 31         cid, result = self.yundama.decode(filename,self. codetype, self.timeout);
 32         return result
 33     def __init__(self,u,p):
 34         self.yzm_csh()
 35         self.username=u
 36         self.password=p
 37         self.s = requests.session()
 38         self.get_login_page()
 39         self.login()
 40         self.s.headers.update({
 41             'Host': 'jwgl.cauc.edu.cn',
 42             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
 43             'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
 44             'Accept-Encoding': 'gzip, deflate',
 45             'Content-Type': 'application/x-www-form-urlencoded',
 46             'keep-alive': 'keep-alive',
 47             'Upgrade-Insecure-Requests': '1'
 48         })
 49     def get_view(self,r):
 50         return re.findall('__VIEWSTATE" value="(.*?)"',r)[0]
 51     def auth(self):
 52         data={
 53         'curl':'Z2F',
 54         'flags':'0',
 55         'forcedownlevel':'0',
 56         'formdir':'3',
 57         'trusted':'0',
 58         'username':'',
 59         'password':'',
 60         }
 61         url='http://jwgl.cauc.edu.cn/CookieAuth.dll?Logon'
 62         res=self.s.post(url,data=data)
 63     def get_login_page(self,):
 64         #__VIEWSTATE变量
 65         res=self.s.get('http://jwgl.cauc.edu.cn')
 66         self.sbm=re.findall('/\((.*?)\)/',res.url)[0]
 67         self.view=self.get_view(res.text)
 68     def get_check_pic(self):
 69         url = 'http://jwgl.cauc.edu.cn/(' + self.sbm + ')/CheckCode.aspx'  # 验证码所在连接
 70         pic = self.s.get(url)
 71         if os.path.exists(r'yanzheng.jpg'):
 72             os.remove(r'yanzheng.jpg')
 73         with open(r'yanzheng.jpg', 'wb')as f:
 74             f.write(pic.content)
 75             f.close()
 76         username = ''
 77         password = ''
 78         #os.startfile(r'yanzheng.jpg')
 79 
 80     def login(self):
 81         while True:
 82             self.get_check_pic()
 83             # ycode = input("输入弹出的验证码: ")
 84             ycode = self.get_cpic_res('yanzheng.jpg')
 85             payload={
 86                      '__VIEWSTATE':self.view,
 87                      'txtUserName':str(self.username),
 88                      'TextBox2':str(self.password),
 89                      'txtSecretCode':ycode,
 90                      'RadioButtonList1':'%D1%A7%C9%FA',
 91                      'Button1':"",
 92                      'lbLanguage':'',
 93                      'hidPdrs':'',
 94                      'hidsc':'',
 95                     }
 96             #更新refer
 97             self.s.headers.update( {'Referer': 'http://jwgl.cauc.edu.cn/(' + self.sbm + ')/Default2.aspx',})
 98             url='http://jwgl.cauc.edu.cn/('+self.sbm+')/Default2.aspx'
 99             r=self.s.post(url,data=payload)
100             #用正则判断登陆情况
101             pat=r'<title>(.*?)</title>'#获取标题的正则表达式
102             x=re.findall(pat,r.text)
103             if(x[0]=="欢迎使用正方教务管理系统!请登录"):print("登陆失败")
104             else:
105                 print("登陆成功")
106                 #抓一下名字
107                 catch='<span id="xhxm">(.*?)</span></em>'
108                 self.name=re.findall(catch,r.text)[0][:-2]
109                 self.xh=re.findall('xh=(.*?)"',r.text)[0]
110                 break
111     def get_grade_all(self):
112         self.get_grade_page()
113         data = {
114             '__VIEWSTATE': self.VIEW,
115             'Button2': '%D4%DA%D0%A3%D1%A7%CF%B0%B3%C9%BC%A8%B2%E9%D1%AF'
116         }
117         self.s.headers.update({ 'Referer': self.url })
118         r = self.s.post(self.url, data=data)
119         print(r.text)
120     def get_grade_page(self):
121         self.url='http://jwgl.cauc.edu.cn/('+self.sbm+')/xscj_gc.aspx?xh='+ self.xh+'&xm='+'%C1%F5%BA%D3'+'&gnmkdm=N121605'
122         self.s.headers.update({'Referer': 'http://jwgl.cauc.edu.cn/('+ self.sbm+')/xs_main.aspx?xh='+ self.xh} )
123         r= self.s.get(self.url)
124         self.VIEW= self.get_view(r.text)
125 
126 edu=Edu('','')
127 edu.get_grade_all()

 

推荐阅读