requests登入知乎新版

來源:互聯網
上載者:User
#coding:utf-8#__author__='wang'import time,json,base64'''requests登陸知乎流程:requests登入知乎流程:1>想到去抓包,查看使用者名稱,密碼錶單資料的提交地址,也就是POST請求將表單資料提交的地址。經過查看是:https://www.zhihu.com/api/v3/oauth/sign_in。2>通過抓取上述登入地址,在其請求的Content欄位中,發現POST到伺服器的地址不只包含使用者名稱,密碼,還有timestamp, lang, client_id, signature等表單資料。所以,需要知道每一個表單資料的特點,而特點我們從資料在每次登入時是否變化來尋找資料的規律。3>經過多次登入的觀察,這些表單資料中,只有timestamp,signature是變化的,其它的值都是不變的。4>通過JS發現signature欄位的值,是由多個欄位組合加密而成,其實timestamp時間戳記是核心,每次根據時間戳記的變化,產生不同的signature值。5>考慮到signature的加密過程較為複雜,所以直接將瀏覽器登入成功後的時間戳記timestamp和簽名signature複製到請求資料中,然後進行登入。6>表單資料填充完畢,發送POST請求時,出現了"缺少驗證碼票據的錯誤(capsion_ticket)",經過分析,驗證碼票據是為擷取驗證碼而提供的一種驗證方式,而抓包工具中關於驗證碼的請求有兩次。一次擷取的是:{'show_captcha': true},而同時請求的第二次擷取的是:{'img_base64': 'Rfadausifupoauerfae'}。7>經過分析,{'show_captcha': true}是擷取驗證碼的關鍵資訊,在抓包資訊中,發現第一次請求的響應中的Set-Cookie中,包含了capsion_ticket驗證碼票據資訊。8>再次類比登入,又出現了"ERR_XX_AUTH_TOKEN"錯誤資訊,而它出現在我們根據驗證碼票據擷取驗證碼圖片時。我們從抓包資訊中,查看關於captcha?lang=cn的請求資訊,發現在要求標頭中有這樣一個欄位:Authorization: oauth ce30dasjfldhjfadsfasdfad。所以將其在headers中進行配置。'''import requeststry:    import cookielibexcept Exception,e:    import http.cookiejar as cookielibsession =requests.Session()session.cookies= cookielib.LWPCookieJar(filename = 'requests_coo.txt')try:    session.cookies.load(ignore_discard=True,ignore_expires=True)    print 'cookie資訊載入成功'except Exception,e:    print "cookie資訊載入失敗"headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0','HOST':'www.zhihu.com','Referer':'https://www.zhihu.com/signin?next=%2F','Authorization':'oauth c3cef7c66a1843f8b3a9e6a1e3160e20'}def zhihu_login(account,password,captcha):    #登陸之前先拿驗證碼資料    # is_captcha = parse_captcha()    # if is_captcha:    #     print '你有驗證碼,我不登陸'    #     # is_captcha =parse_captcha()    # else:    #     print '沒有驗證碼,我登陸'    post_url='https://www.zhihu.com/api/v3/oauth/sign_in'    post_data={        'client_id':'c3cef7c66a1843f8b3a9e6a1e3160e20',        'grant_type':'password',        'timestamp':'1515398025518',        'source':'com.zhihu.web',        'signature':'30b129980d00e5efb09f16b0334bf4e8601b060b',        'username':account,        'password':password,        'captcha':captcha,        'lang':'en',        'ref_source':'homepage',        'utm_source':''    }    response = session.post(post_url,data=post_data,headers=headers,verify=False)    session.cookies.save(ignore_expires=True,ignore_discard=True)#如果直接登入會報一個異常“缺少驗證碼票據”,而驗證碼票據的擷取是通過向https://www.zhihu.com/api/v3/oauth/captcha?lang=cn地址發送get請求之後,伺服器會將驗證碼票據放入set-cookie中進行返回def parse_captcha():    response = session.get('https://www.zhihu.com/api/v3/oauth/captcha?lang=en',headers=headers,verify= False)    show_captcha=response.json()['show_captcha']    if show_captcha:        print '有驗證碼'        #有驗證碼,就再一次向https://www.zhihu.com/api/v3/oauth/captcha?lang=en發送put請求,用於向伺服器索引當前的驗證碼的圖片地址        response = session.put('https://www.zhihu.com/api/v3/oauth/captcha?lang=en',headers=headers,verify=False)        try:            img=json.loads(response.content)['img_base64']        except Exception,e:            print '擷取img_base64的值失敗,原因:'%e        else:            print '成功擷取加密後的圖片地址'            #將加密後的圖片進行解密,同時儲存到本地            img = img.encode('utf-8')            img_data = base64.b64decode(img)        with open('zhihu_captcha.GIF','wb') as f:            f.write(img_data)            captcha = raw_input('請輸入識別的驗證碼:')        #將驗證碼繼續發送post請求和伺服器端進行對比是否正確        data = {'input_text':captcha}        response = session.post('https://www.zhihu.com/api/v3/oauth/captcha?lang=en',data = data,headers=headers,verify=False)        try:            yanzheng_result = json.loads(response.content)['success']        except Exception,e:            print '關於驗證碼的post請求響應失敗,原因:{}'.format(e)        else:            if yanzheng_result:                zhihu_login('****','*****,captcha)            else:                print '是錯誤的驗證碼'        # return True    else:        print '沒有驗證碼'        zhihu_login('*****','******',captcha='')parse_captcha()#SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed(_ssl.c:661))requests庫在訪問http://協議時,由於認證認證失敗,無法訪問相應的服務這個時候可以通過verify=False設定禁止認證認證的過程。urllib2在訪問https://的時候也會只有這種異常出現# zhihu_login('15518325965','yun0101@.29')def get_index_page():    response = session.get('https://www.zhihu.com',headers=headers,verify = False)    with open('index.html','w') as f:        f.write(response.content)    print '擷取資訊成功'# get_index_page()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.