python 類比知乎登入,包含驗證碼

來源:互聯網
上載者:User

標籤:ext   6.4   wan   windows   ima   mail   3.0   else   pre   

#!/usr/bin/env python3# -*- coding: utf-8 -*-‘‘‘Required- requests (必須)- pillow (可選)Info- author : "xchaoinfo"- email  : "[email protected]"- date   : "2016.2.4"Update- name   : "wangmengcn"- email  : "[email protected]"- date   : "2016.4.21"‘‘‘import requeststry:    import cookielibexcept:    import http.cookiejar as cookielibimport reimport timeimport os.pathtry:    from PIL import Imageexcept:    pass# 構造 Request headersagent = ‘Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0‘headers = {    ‘User-Agent‘: agent}# 使用登入cookie資訊session = requests.session()session.cookies = cookielib.LWPCookieJar(filename=‘cookies‘)try:    session.cookies.load(ignore_discard=True)except:    print("Cookie 未能載入")def get_xsrf():    ‘‘‘_xsrf 是一個動態變化的參數‘‘‘    index_url = ‘http://www.zhihu.com‘    # 擷取登入時需要用到的_xsrf    index_page = session.get(index_url, headers=headers)    html = index_page.text    pattern = r‘name="_xsrf" value="(.*?)"‘    # 這裡的_xsrf 返回的是一個list    _xsrf = re.findall(pattern, html)    return _xsrf[0]# 擷取驗證碼def get_captcha():    t = str(int(time.time()*1000))    captcha_url = ‘http://www.zhihu.com/captcha.gif?r=‘ + t + "&type=login"    r = session.get(captcha_url, headers=headers)    with open(‘captcha.jpg‘, ‘wb‘) as f:        f.write(r.content)        f.close()    # 用pillow 的 Image 顯示驗證碼    # 如果沒有安裝 pillow 到原始碼所在的目錄去找到驗證碼然後手動輸入    try:        im = Image.open(‘captcha.jpg‘)        im.show()        im.close()    except:        print(u‘請到 %s 目錄找到captcha.jpg 手動輸入‘ % os.path.abspath(‘captcha.jpg‘))    captcha = input("please input the captcha\n>")    return captchadef isLogin():    # 通過查看使用者個人資訊來判斷是否已經登入    url = "https://www.zhihu.com/settings/profile"    login_code = session.get(url,allow_redirects=False).status_code    if int(x=login_code) == 200:        return True    else:        return Falsedef login(secret, account):    # 通過輸入的使用者名稱判斷是否是手機號    if re.match(r"^1\d{10}$", account):        print("手機號登入 \n")        post_url = ‘http://www.zhihu.com/login/phone_num‘        postdata = {            ‘_xsrf‘: get_xsrf(),            ‘password‘: secret,            ‘remember_me‘: ‘true‘,            ‘phone_num‘: account,        }    else:        print("郵箱登入 \n")        post_url = ‘http://www.zhihu.com/login/email‘        postdata = {            ‘_xsrf‘: get_xsrf(),            ‘password‘: secret,            ‘remember_me‘: ‘true‘,            ‘email‘: account,        }    try:        # 不需要驗證碼直接登入成功        login_page = session.post(post_url, data=postdata, headers=headers)        login_code = login_page.text        print(login_page.status)        print(login_code)    except:        # 需要輸入驗證碼後才能登入成功        postdata["captcha"] = get_captcha()        login_page = session.post(post_url, data=postdata, headers=headers)        login_code = eval(login_page.text)        print(login_code[‘msg‘])    session.cookies.save()try:    input = raw_inputexcept:    passif __name__ == ‘__main__‘:    if isLogin():        print(‘您已經登入‘)    else:        account = input(‘請輸入你的使用者名稱\n>  ‘)        secret = input("請輸入你的密碼\n>  ")        login(secret, account)

 

python 類比知乎登入,包含驗證碼(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.