V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yhf
V2EX  ›  Python

python 模拟登录不成功, 求大神帮助!!!

  •  
  •   yhf · 2014-02-19 14:26:53 +08:00 · 5499 次点击
    这是一个创建于 3724 天前的主题,其中的信息可能已经有所发展或是发生改变。
    今天想做个小DEMO, 把自己教务处里的成绩进行分析, 做一个可视化. 可是没想到卡在了第一步....

    以前用python模拟登录的时候都是两分钟搞定的, 今天模拟登录教务处居然弄了两个小时也没成功. 教务处的网址在这里: http://202.117.120.37/xdjwWebNew/index.jsp

    我的代码:
    # -*- coding: utf-8 -*-
    import requests

    s = requests.Session()
    login_data = {
    'uid': 'xxxxxxx',
    'password':'xxxxxxxxx',
    'command': 'studentLogin',
    'sltType': '学生',
    'Submit': '确定'
    }

    header = {
    'Host': '202.117.120.37',
    'Proxy-Connection': 'keep-alive',
    'Content-Length': '95',
    'Cache-Control': 'max-age=0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Origin': 'http://202.117.120.37',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Referer': 'http://202.117.120.37/xdjwWebNew/systemAdmin/Login.jsp?command=studentLogin',
    'Accept-Encoding': 'gzip,deflate,sdch',
    'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6'
    }
    s.post('http://202.117.120.37/xdjwWebNew/Servlet/UsersControl', data = login_data, headers = header)
    r = s.get('http://202.117.120.37/xdjwWebNew/main_study.jsp')
    print r.text.encode('utf-8')


    不知道错在哪里, 返回的总是一个乱七八糟的网站....
    30 条回复    1970-01-01 08:00:00 +08:00
    nodejx
        1
    nodejx  
       2014-02-19 15:30:16 +08:00
    LZ可以把“s.post('http://202.117.120.37/xdjwWebNew/Servlet/UsersControl', data = login_data,headers=header)”修改成“r=s.post('http://202.117.120.37/xdjwWebNew/Servlet/UsersControl', data = login_data, headers = header)”,然后print r.url看下跳转到的url是否正确。
    Comdex
        2
    Comdex  
       2014-02-19 15:37:17 +08:00
    同样想模拟学校的登录正方教务系统,求楼主指点
    yhf
        3
    yhf  
    OP
       2014-02-19 15:44:50 +08:00
    @nodejx 我已经发现猫腻了, 我用下面的代码查看登录页面的原代码, 返回的居然是另一个网页!

    import urllib2

    command=studentLogin'
    url = 'http://202.117.120.37/xdjwWebNew/systemAdmin/Login.jsp?command=studentLogin'
    html = urllib2.urlopen(url).read()
    print html

    这是什么情况!.......
    nodejx
        4
    nodejx  
       2014-02-19 16:16:33 +08:00
    nodejx
        5
    nodejx  
       2014-02-19 16:40:14 +08:00
    @加入header字段就可以使用requests来get上面的页面了,urllib2我不熟,应该也是可以加入header参数的吧。
    nodejx
        6
    nodejx  
       2014-02-19 16:42:14 +08:00
    @yhf应该是urllib2连接超时会自动跳转到docs.python.org/2/library/urllib2.html这个页面。
    blueandhack
        7
    blueandhack  
       2014-02-19 17:35:51 +08:00
    @yhf 试着用wireshark抓包看看,到底post的是哪个地址。
    blueandhack
        8
    blueandhack  
       2014-02-19 17:36:37 +08:00
    @yhf 还可以试试使用Cookie欺骗
    tan9le
        9
    tan9le  
       2014-02-19 17:40:12 +08:00
    var sharedValue = -333181683
    function change(){
    var pw = document.form1.password.value;

    pw = hex_md5(pw);
    //alert(pw+sharedValue);
    pw = hex_md5(pw+sharedValue); //用共享数值再次加密
    //document.write(pw);
    document.form1.password.value = pw ;
    // return true;
    }
    话说排除你那诡异的提交地址的问题之外,页面上的javascript加密中sharedValue是每次页面刷新都会变的,请注意
    zhy0216
        10
    zhy0216  
       2014-02-19 17:44:29 +08:00
    很奇怪的样子,
    真不行我们有: https://github.com/jeanphix/Ghost.py
    zhy0216
        11
    zhy0216  
       2014-02-19 17:46:36 +08:00
    @tan9le 原来是这样! 这个password的value总是在变的啊
    fucker
        12
    fucker  
       2014-02-19 18:18:48 +08:00
    xdjw,西电教务,哥们也西安的啊
    yhf
        13
    yhf  
    OP
       2014-02-19 19:20:21 +08:00
    @tan9le 恩 这个已经注意了
    yhf
        14
    yhf  
    OP
       2014-02-19 19:23:46 +08:00
    @zhy0216 好东西
    yhf
        15
    yhf  
    OP
       2014-02-19 19:24:09 +08:00   ❤️ 1
    @nodejx 好 我试试
    yhf
        16
    yhf  
    OP
       2014-02-19 19:25:16 +08:00
    @blueandhack post的地址不就是chrome里查到的request的那个地址吗?
    Tink
        17
    Tink  
       2014-02-19 19:28:57 +08:00 via iPad
    额。。我老早以前做过。。。。同西电
    intosec
        18
    intosec  
       2014-02-19 19:31:50 +08:00
    用浏览器看看登陆请求POST的是什么数据,看看是否对password进行了加密处理。
    PS:这个网站上直接就有猪肉店
    yhf
        19
    yhf  
    OP
       2014-02-19 19:39:28 +08:00
    @yhf 能看一下你的代码吗?
    yhf
        20
    yhf  
    OP
       2014-02-19 19:40:06 +08:00
    @intosec post的数据已经搞出来了 关键是post以后它会自动跳转
    intosec
        21
    intosec  
       2014-02-19 19:46:09 +08:00
    @yhf 那要是不管它的跳转,直接用登陆后获得的cookie去打开POST学生成绩的url呢?
    zsj950618
        22
    zsj950618  
       2014-02-19 19:50:44 +08:00
    教务系统有验证码的怎么破。。。
    yhf
        23
    yhf  
    OP
       2014-02-19 20:02:05 +08:00
    @intosec 额 我不会写...不过觉着这样应该可行
    blueandhack
        24
    blueandhack  
       2014-02-19 21:49:10 +08:00
    yutify
        25
    yutify  
       2014-02-19 21:50:03 +08:00 via iPhone
    @blueandhack 为什么我5分钟前发的主题直接沉底呢?没有显示在最上面 http://v2ex.com/t/100970
    blueandhack
        26
    blueandhack  
       2014-02-19 21:51:25 +08:00
    @yhf 用的Cookie登陆的
    crazycookie
        27
    crazycookie  
       2014-02-21 09:05:15 +08:00
    没关系的,主要是,你要获得cookies,至于登录后他登录到那边无所谓的。你只要能有权限浏览你想要的页面即可
    不管他登录后跳转到什么页面,获得cookies,然后把cookies放到新的请求中去获得 你想要的页面。
    PS:我到现在碰到的最噁心的方式,http auth, cookie 登录, 外加ip和 user agent 验证. 这都被我绕过去了。

    关于 表单参数一直变的问题的回答是:有办法。
    用Ghost.py. 可以用js去控制填写. 然后把他的返回的dom转换成html即可。
    PS again: js的执行结果也在里面哦

    做得细致点,伪造下 user-agent, 代理下ip, 基本可以模拟人的访问行为动作了.
    算是黑技能了(我好像暴露了什么)
    reverland
        28
    reverland  
       2014-02-22 22:07:48 +08:00
    @zsj950618 教务的验证码,如果是指正方教务那种,相当简单,之前我们学校做个[https://github.com/reverland/scripts/tree/master/python/captcha_cl](https://github.com/reverland/scripts/tree/master/python/captcha_cl).如果是谷歌那种,人工吧……

    另外,自从开始用selenium……再也不想自己去打开firebug研究模拟登录了……
    reverland
        29
    reverland  
       2014-02-22 22:09:16 +08:00
    @zsj950618 selenium可以截屏……截屏时顺便把验证码解决就行……也许有更好的办法。
    reverland
        30
    reverland  
       2014-02-22 22:11:48 +08:00
    @zsj950618 我忽然想起来,正方教务有好几个入口 aspx1 aspx2 aspx3等等,一般只有一个入口要验证码……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2191 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:34 · PVG 18:34 · LAX 03:34 · JFK 06:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.