推荐学习书目
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
ranvane
V2EX  ›  Python

请大家给我看一下这个网站如何使用 python 模拟登陆?

  •  
  •   ranvane · Jan 12, 2015 · 5998 views
    This topic created in 4156 days ago, the information mentioned may be changed or developed.
    工作任务突然下来了,好多,考虑了一下想使用python登陆获取数据,然后和本地系统数据对比做一下,但是我发现我使用requesrs模拟登陆竟然登陆失败,百思不得其解,只好向大家求助。
    网址: http://61.178.81.162:8888/LQL_OA/login.aspx
    因为保密的原因,账号和密码不能提供,请大家谅解,如果因为客观条件不能够提供代码,请达人提供一下思路,谢谢!

    # -*- encoding: utf-8 -*-

    import requests
    from PIL import Image
    from StringIO import StringIO
    from lxml import etree
    from lxml import *
    import lxml.html
    import urllib2
    import lxml.html as H

    txtUserName = ""
    txtPassword = ""
    yz = ''#验证码字符串

    signin_url = "http://61.178.81.162:8888/LQL_OA/login.aspx"
    login_url = "http://61.178.81.162:8888/LQL_OA/Index.aspx"
    LoginCode_url ="http://61.178.81.162:8888/LQL_OA/LoginCode.aspx"
    user_agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)"
    Referer = signin_url

    __EVENTARGUMENT=''
    __EVENTTARGET = 'lkbut_OK'
    __VIEWSTATE = ''
    __VIEWSTATEGENERATOR='17AE1282'


    login = requests.get(signin_url)
    doc = H.document_fromstring(login.content)
    __VIEWSTATE = doc.xpath('//*[@id="__VIEWSTATE"]/@value')[0]

    print __VIEWSTATE

    #下载验证码图片
    yz_LoginCode = requests.get(LoginCode_url)

    im = Image.open(StringIO(yz_LoginCode.content))
    im.show()
    yz = input("Please input your Code:\n")

    #登陆网页
    headers = {"User-Agent": user_agent,'Referer': Referer,}


    logininfo = {"txtUserName": txtUserName,
    "txtPassword": txtPassword,
    "yz": yz,
    "__EVENTARGUMENT":__EVENTARGUMENT,
    "__EVENTTARGET":__EVENTTARGET ,
    "__VIEWSTATE":__VIEWSTATE,
    "__VIEWSTATEGENERATOR":__VIEWSTATEGENERATOR,
    }

    liudong_session = requests.Session()
    signin_req = liudong_session.post(signin_url,
    data=logininfo,
    headers=headers,
    )

    print signin_req.encoding


    content = open('content.html', 'w')

    content.write(signin_req.content.replace('/LQL_OA','http://61.178.81.162:8888/LQL_OA'))
    content.close()

    info = liudong_session.get(login_url)

    info_content = open('info_content.html', 'w')
    info_content.write(info.content.replace('/LQL_OA','http://61.178.81.162:8888/LQL_OA'))
    info_content.close()
    7 replies    2015-01-12 18:10:14 +08:00
    ihciah
        1
    ihciah  
       Jan 12, 2015
    urllib2配上cookiejar
    ozking
        2
    ozking  
       Jan 12, 2015
    beshe
        3
    beshe  
       Jan 12, 2015
    你至少要先找到那个地方有问题 才行啊,这类问题不外乎: header, cookie, session, 登录,验证码,页面解析 这几个方面了。。。
    kaneg
        4
    kaneg  
       Jan 12, 2015
    用firebug将登陆的request,response详细信息抓下来,然后跟你的请求对比。或者更好的办法是用http 抓包工具,比如fiddler比较差异
    Cynic222
        5
    Cynic222  
       Jan 12, 2015
    puyo
        6
    puyo  
       Jan 12, 2015
    应该是验证码那儿出了问题。
    Sylv
        7
    Sylv  
       Jan 12, 2015
    因为你获取验证码的时候用的是 requests.get,后面登录的时候用的是 requests.Session(),两次不是同一个 session,导致登录的时候验证码不一样了。
    解决办法:从头到尾用同一个 s = requests.Session(),验证码 yz_LoginCode = s.get(LoginCode_url),登录 signin_req = s.post(signin_url, data=logininfo, headers=headers)。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 19:27 · PVG 03:27 · LAX 12:27 · JFK 15:27
    ♥ Do have faith in what you're doing.