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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章