python获取登录验证码

根据sessionId下载验证码,通过人工方式识别

import urllib2

def get_captcha(captchaUrl, sessionIdKey, sessionIdValue):
    """获得验证码图片

    通过fiddler分析验证码请求时需要携带的cookie,通常验证码是与某个sessionid绑定的


    Args:
        captchaUrl: 验证码地址
        sessionIdKey: session key
        sessionIdValue: session value
    """
    req = urllib2.Request(captchaUrl)
    req.add_header("Cookie","%s=%s" % (sessionIdKey,sessionIdValue))
    res = urllib2.urlopen(req)
    picture = res.read()
    imgpath = "captcha.gif"
    with open(imgpath,"wb") as fp:
        fp.write(picture)

    # 请输入验证码
    captchaValue = raw_input("please input captcha:")
    return captchaValue

你可能感兴趣的:(python,python,验证码)