Requests Login to know the new version

Source: Internet
Author: User
Tags base64 oauth ticket
#coding: Utf-8 #__author__ = ' Wang ' import time,json,base64 ' ' requests landing know the process: Requests login know the flow: 1> thought to grab the bag, view the user name,
The submission address of the password form data, which is the address that the POST request will submit the form data to.
After viewing is: https://www.zhihu.com/api/v3/oauth/sign_in. 2> by crawling the above login address, in its requested content field, found that the post to the server address not only contains user name, password, as well as timestamp, Lang, client_id, signature and other forms of data.
So, you need to know the characteristics of each form data, and the characteristics of our data from each login if the change to find the law of the data.
3> After many login observations, these form data, only timestamp,signature is changed, the other values are unchanged.
4> through JS found signature field value, is composed of multiple field combination encryption, in fact, timestamp timestamp is the core, each time according to the change of timestamp, generate different signature values.
5> considering the complexity of the signature encryption process, copy the timestamp timestamp and signature signature after successful browser login to the request data, and then log in. 6> form data is filled in, send a POST request, there is a "Missing verification Code ticket error (Capsion_ticket)", after analysis, the verification code ticket is to obtain the authentication code and provide a way to verify, and grab the bag tool about the authentication code request has two times.
One fetch was: {' Show_captcha ': true}, while the second request was fetched: {' img_base64 ': ' Rfadausifupoauerfae '}.
7> after analysis, {' Show_captcha ': true} is the key information to obtain the verification code, in the grab packet information, found in the first request in the response of the Set-cookie, contains the Capsion_ticket authentication code ticket information. 8> again simulates the login, and there is a "Err_xx_auth_token" error message, which appears when we obtain the CAPTCHA image based on the CAPTCHA ticket. From the grab information, we view the request information about CAPTCHA?LANG=CN and found that there is a field in the request header: Authorization:oauth CE30dasjfldhjfadsfasdfad.

Therefore, it is configured in headers. "' Import requests Try:import cookielib except Exception,e:import Http.cookiejar as Cookielib session =reques Ts. Session () session.cookies= Cookielib. Lwpcookiejar (filename = ' requests_coo.txt ') try:session.cookies.load (ignore_discard=true,ignore_expires=true) PR int ' cookie information loaded successfully ' except Exception,e:print ' cookie information failed to load 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): # Get authentication code data before landing # Is_captcha = Parse_captcha () # If Is_captcha: # print ' You have captcha, I don't login ' # # Is_captcha =p Arse_captcha () # Else: # print ' No captcha, I landed ' 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 ': ' 30b129980d
        00e5efb09f16b0334bf4e8601b060b ', ' username ': account, ' password ':p assword, ' 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) #
If a direct login will report an exception "missing Authenticode Ticket", and the acquisition of the CAPTCHA ticket is done by sending a GET request to the HTTPS://WWW.ZHIHU.COM/API/V3/OAUTH/CAPTCHA?LANG=CN address, The server will put the CAPTCHA ticket in the Set-cookie to return 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:pri NT ' have authentication code ' #有验证码, once again to Https://www.zhihu.com/api/v3/oauth/captcha lang=en send put request, used to index the server to the current authentication code picture address Respon SE = session.put (' Https://www.zhihu.com/api/v3/oauth/captcha?lAng=en ', Headers=headers,verify=false) try:img=json.loads (response.content) [' Img_base64 '] exce PT Exception,e:print ' Get img_base64 value failed because: '%e Else:print ' successfully acquired encrypted picture address ' #将加密 After the image is decrypted and saved to the local img = Img.encode (' utf-8 ') img_data = Base64.b64decode (img) with open (' Zhi Hu_captcha. GIF ', ' WB ') as F:f.write (img_data) captcha = raw_input (' Please enter a recognized authentication code: ') #将验证码继续发送post请求和服务器端进 Row comparison is correct 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 request for Authenticode failed to respond because: {} '. Format (e) else:if
        Yanzheng_result:zhihu_login (' * * *, ' *****,captcha ') Else:print ' is the wrong authentication code ' # return True else:print ' No Authentication Code ' zhihu_login (' * * *, ' hu ', captcha= ') Parse_captcha () #SSLError (sslerr
or (1, U ' [ssl:certificate_verify_failed] certificate VERIFY FAILED (_ssl.c:661)) requests library when accessing the http://protocol, because certificate authentication failed, Unable to access the appropriate service at this time, you can set the process of prohibiting certificate authentication through Verify=false. URLLIB2 will only see this anomaly when visiting 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 (respons
 e.content) print ' Get information success ' # Get_index_page ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.