开发者社区> 问答> 正文

Python爬虫 湖大教学服务系统 表单提交出错:报错

IE浏览器,用Fiddler抓包,模拟登陆过程:

然后我就很纳闷为啥右边的表单里面啥都没有。。包括我输入的用户名、密码和验证码。。

import urllib.request
import http.cookiejar
import re
from bs4 import BeautifulSoup

class HNU:
    #初始化:url、cookies、headers
    def __init__(self):

        self.loginUrl='http://hdjw.hnu.cn/_data/index_login.aspx'
        
        self.loginPage='http://hdjw.hnu.cn/default.aspx'
        #cookies的使用
        self.cookies=http.cookiejar.MozillaCookieJar()
        self.handler=urllib.request.HTTPCookieProcessor(self.cookies)
        self.opener=urllib.request.build_opener(self.handler)
        self.headers={
            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding' : 'gzip, deflate',
            'Accept-Language':'zh-CN,zh;q=0.8',
            'Cache-Control':'max-age=0',
            'Content-Length':'5330',
            'Content-Type':'application/x-www-form-urlencoded',
            'Cookie':'ASP.NET_SessionId=xuwklzyk34hfg5za5rim2n55',
            'Host':'hdjw.hnu.cn',
            'Origin:http':'//hdjw.hnu.cn',
            'Proxy-Connection':'keep-alive',
            'Referer:http':'//hdjw.hnu.cn/_data/index_login.aspx',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 LBBROWSER'
            }
        
    '''   
    def getInitPage(self):
        
        try:
            response=self.opener.open(self.loginPage)
            data=response.read().decode('gbk')
            return data
        except urllib.error.URLError as error:
            print ('连接湖南大学教学服务系统失败,错误原因:'),error.reason
            return None
    '''

    #手动获取验证码
    def getSecurityCode(self):
        
        im_url='http://hdjw.hnu.cn/sys/ValidateCode.aspx'
        im_data=self.opener.open(im_url).read()
        f=open('Code.png','wb')
        f.write(im_data)
        f.close()
        validateCode=input()
        #print (self.cookies)
        return str(validateCode)

    #构造post数据,提交
    def getPostPage(self, code, username, password):
        
        postDict={
                'txt_dsdsdsdjkjkjc':username,
                'txt_dsdfdfgfouyy':password,
                'txt_ysdsdsdskgf':code,
        
            }
        postData = urllib.parse.urlencode(postDict).encode()
        try:
            req = urllib.request.Request(self.loginUrl, postData,self.headers)
            response = self.opener.open(req)
            data = response.read().decode('gbk')
            return data
        except urllib.error.URLError as error:
            print (error.reason)
            return None
        
    
    
#test main
hnu=HNU()
code=hnu.getSecurityCode()
data=hnu.getPostPage(code, "此处账号", "此处密码")
print (data)



输入验证码后,返回的网页提示:未知错误

总结就是:为啥别的网站模拟登陆后可以看到自己提交的表单内容(账号密码啥的),这个网站就不行。。

展开
收起
kun坤 2020-06-05 22:55:06 1641 0
1 条回答
写回答
取消 提交回答
  • 不要以为所有的post都是明码递交,你也看看递交按钮的代码啊,肯定有js混淆。

    粗略看了一下页面源码,登录框是一个iframe(果然是古老的设计),嵌套的页面在这里呢: http://hdjw.hnu.cn/_data/index_login.aspx

    打开chrome开发者工具,可以看到这个页面请求还有个md5.js,没细读,应该是用于混淆用户名和密码用。

    大致看了一下页面源码,递交post函数在这里:

               else { document.getElementById('divLogNote').innerHTML='正在通过身份验证...请稍候!';
    
    		    Ajax.doPost("index_login.aspx","Sel_Type="+document.all.Sel_Type.value+"&werereruuyyuxcxcx="+document.all.werereruuyyuxcxcx.value+"&efdfdfuuyyuuckjg="+document.all.efdfdfuuyyuuckjg.value+"&roleCHK="+roleCHK+"&typeName="+document.all.typeName.value,fillOptions,true);
    		    document.all.txt_dsdsdsdjkjkjc.value=''; 
    		    document.all.txt_dsdfdfgfouyy.value='';
    		    document.all.txt_ysdsdsdskgf.value='';
    		    }
    		 }
    		 function fillOptions()
    		 {
    		     
    		 }
    



    利用Ajax向http://hdjw.hnu.cn/_data/index_login.aspx发起post请求,请求的主体就是后面那几个字符串拼接的东西。So,去读源码吧,看看这几个函数的算法是什么,用python实现了这个几个函数的算法即可。Over

    ######回复 @BigGhost : 给个联系方式啊,我最近也在弄这个,我们的教务系统好像是一样的。。。######谢谢哈!思路敞开多了。
    2020-06-05 22:55:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Improving Python and Spark Per 立即下载
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载