Python模拟登陆大连交通大学教务在线

Python模拟登陆大连交通大学教务在线,使用wxPython做的界面,urllib2发送数据,可以实现登陆后获取首页登陆数据的功能,目前只做登陆,其他的暂时没做

Python2.7代码

# -*- coding: UTF-8 -*-
import wx
import httplib2
import cStringIO
import urllib
import random

class ExamplePanel(wx.Panel):
    __h = httplib2.Http()
    __headers = {'Cookie': ''}

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.jwtitle = wx.StaticText(self, label=u"大连交通大学教务在线模拟登陆", pos=(20,30))
        self.lblname = wx.StaticText(self, label=u"用户名:", pos=(20,60))
        self.editname = wx.TextCtrl(self, value="", pos=(100, 60), size=(140,-1))
        self.lblpass = wx.StaticText(self, label=u"密  码:", pos=(20,90))
        self.editpass = wx.TextCtrl(self, value="", pos=(100, 90), size=(140,-1), style=wx.TE_PASSWORD)
        self.lblchpt = wx.StaticText(self, label=u"验证码:", pos=(20,120))
        self.editchtp = wx.TextCtrl(self, value="", pos=(100, 120), size=(140,-1))

        #加载验证码图片

        url = 'http://jw.djtu.edu.cn/academic/common/security/login.jsp?'
        resp, content = self.__h.request(url)
        cookiestr = resp['set-cookie'].split('; ')[0]  # 响应的cookie多了路径path
        print cookiestr
        self.__headers={'Cookie': cookiestr}
        url = 'http://jw.djtu.edu.cn/academic/getCaptcha.do'
        print url
        resp, content = self.__h.request(url, 'GET',headers=self.__headers)
        stream = cStringIO.StringIO(content)
        bmp = wx.BitmapFromImage( wx.ImageFromStream(stream))
        self.sbmp = wx.StaticBitmap(self, -1, bmp, (20, 150))

        #刷新验证码按钮
        self.btnref =wx.Button(self, label=u"刷新验证码", pos=(150, 150))

        self.btnsub =wx.Button(self, label=u"提  交", pos=(150, 200))
        self.Bind(wx.EVT_BUTTON, self.OnClickRefresh,self.btnref)
        self.Bind(wx.EVT_BUTTON, self.OnClickSubmint,self.btnsub)


    def OnClickRefresh(self,event):
        print 'clickRefresh'

        url = 'http://jw.djtu.edu.cn/academic/getCaptcha.do?'+str(random.random())
        print url
        resp, content = self.__h.request(url, 'GET',headers=self.__headers)
        stream = cStringIO.StringIO(content)
        bmp = wx.BitmapFromImage( wx.ImageFromStream(stream))
        self.sbmp=wx.StaticBitmap(self, -1, bmp, (20, 150))


    def OnClickSubmint(self,event):
        print 'clickSubmint'

        # 必须编码成网页表单提交,不能直接作为POST的数据域提交
        body = {'groupId': '', 'j_username': self.editname.GetValue(), 'j_password': self.editpass.GetValue(), 'j_captcha': self.editchtp.GetValue()}
        postheaders = self.__headers
        postheaders['Content-type']='application/x-www-form-urlencoded'
        url = 'http://jw.djtu.edu.cn/academic/j_acegi_security_check'
        resp, content = self.__h.request(url, 'POST', headers=postheaders, body=urllib.urlencode(body))
        print resp #这个响应是重定向304

        redirecurl = resp['location'] # 登陆之后会重定向
        resp, content = self.__h.request(redirecurl, 'GET', headers=self.__headers)
        print resp
        charset=resp['content-type'].split('charset=')[-1]  # 提取字符编码
        print content.decode(charset).encode('utf-8')

        url = 'http://jw.djtu.edu.cn/academic/showHeader.do'
        resp, content = self.__h.request(url, 'GET', headers=self.__headers)
        print resp
        charset = resp['content-type'].split('charset=')[-1]
        print content.decode(charset).encode('utf-8')


app = wx.App(False)
frame = wx.Frame(None)
panel = ExamplePanel(frame)
frame.Show()
app.MainLoop()

使用效果

点击登陆之后,登陆之后的相关网页信息会输出到控制台



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值