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