Python_ Demo Login

Source: Internet
Author: User

How do I log in to know?

First of all to analyze, for the verification of the time, the server need us to submit what data, submit the address. A few login attempts first, through the browser in the network to view the data stream, the simulation login is required to provide 5 data, respectively, _XSRF, password, captcha_type, CAPTCHA, Phone_num, this is the mobile phone number to verify the login, Submit address Https://www.zhihu.com/login/phone_num, that, start to get these data, one of the image verification code is the most rare, through the third-party plug-in Zheye Verification code parsing, you can solve the problem

How to do?

1. Request to know the login page and resolve to get _XSRF value

2. Request the Verification code URL to get the captcha picture, turn a zheye to parse the result and stitch out the CAPTCHA value

3. Get the Captcha_type value from the verification submission form and get the form submission address

4. Captcha_type, password the user name and password

5. Submit the data, get the returned response, by judging whether the response data gets the login success

#!/usr/bin/python3import requestsimport reimport jsonfrom time import sleepfrom random import choice, Randintfrom zhihu_y Anzheng.zheye Import zheyeheaders = {' pragma ': ' no-cache ', ' Referer ': ' Https://www.zhihu.com/signin ', ' User-agen ' T ': ' mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/59.0.3071.115 safari/537.36 '}# generate global Session Value session = Requests.session () def random_sleep (): # random pause, impersonate a person to access while choice ([0, 1]): Sleep (choice ([0.1, 0.2, 0.3, 0.4, 0. 5, 0.6, 0.7, 0.8]) def GET_XSRF (): "" "gets the value of the page _xsrf:p Aram URL:: return:" "" Zhihu_url = ' https://www.z Hihu.com/signin ' # Build mappings to increase the readability of the program Xsrf_value = 1 while true:random_sleep () resp = Session.get (Zhihu_url, headers=headers) # Check if return correct resp.raise_for_status () # set page encoding Resp.encod ing = resp.apparent_encoding # get _xsrfde value _xsrf = Re.match (R '. *name= "_XSRF" value= "(. *?)" ', Re.sub (R ' [\r\t\n] ', ' ', Resp.text '). Group (Xsrf_value) # guaranteed _XSRF data if _xsrf:break return _xsrfdef Get_img_dat        A (): "" "gets the picture and obtains the verification code data for the picture: return:" "While True:random_int = str (randint (1, POW (10, 13)))                # Get the value of the picture Img_url = ' HTTPS://WWW.ZHIHU.COM/CAPTCHA.GIF?R=%S&TYPE=LOGIN&LANG=CN '% random_int Random_sleep () # get Picture Img_resp = Session.get (Img_url, Headers=headers) with open (' zhihu_img.jpg ', ' WB ') as F:f.write (img_resp.content) # parse Picture z = Zheye () Img_yanzhe = Z.recognize (' zhihu_img. JPG ') # to sort the obtained coordinates by x [(48.647850377664284, 315.97586850515023), (49.944977855563351, 146.27730894630022)] Im G_yanzhe.sort (Key=lambda x:x[1]) # Knowing the location data submitted and Zheye parsing the data location instead, replace the data with the information required img_data = [] for y, x in img _yanzhe: # Zheye picture is 400*88 image number, the requirement is 200*44, all values are divided by 2 img_data.append ((X/2, Y/2)) # There is data representation parsing        Success, no data re-request data to parse againIf Img_data:break return img_datadef get_captcha_data (img_data): "" is formatted by string to get the desired captcha value "" "# Capt cha:{"Img_size": [200,44], "input_points": [[[120.375,34],[160.375,36]]} # First, second, third corresponding to number one, second, and third values, X, y corresponds to X. Y coordinate value first, second, third, x, y = 0, 1, 2, 0, 1 if len (img_data) = = 1:captcha = ' {' img_size ': [200,44], ' input _points ": [[%.2f,%.2f]]} '% (Img_data[first][x], img_data[first][y]) elif len (img_data) = = 2:c Aptcha = ' {' img_size ': [200,44], ' input_points ': [[%.2f,%.2f],[%.2f,%.2f]]} '% (Img_data[first][x], Img_dat A[first][y], img_data[second][x], img_data[second][y]) elif len (img_data) = = 2:captcha = ' {' img_size ': [200,44], "Input_points": [[%.2f,%.2f],[%.2f,%.2f],[%.2f,%.2f]]} '% (Img_data[first][x], img_data[first][y], Img_da TA[SECOND][X], Img_data[second][y], img_data[third][x], Img_data[third][y]) return captchadef get_form_data (): "" "gets The desired form data "" "_XSRF = get_XSRF () Img_data = Get_img_data () Captcha = Get_captcha_data (img_data) form_data = {' _xsrf ': _XSRF,    ' Password ': ' Password ', ' captcha_type ': ' cn ', ' Captcha ': captcha, ' phone_num ': ' Phone_num ',} Return form_datadef Go_login (post_url): "" "Submit form and get return value" "" From_data = Get_form_data () random_sleep () # Submit Validation Information, redirect Resp_yan = Session.post (Post_url, Headers=headers, Data=from_data, Allow_redirects=false) # Gets the value returned ResU lt = resp_yan.text return json.loads (result) if __name__ = = ' __main__ ': Post_url = ' Https://www.zhihu.com/login/phone _num ' result = Go_login (post_url) print (result)

  

Python_ Demo Login

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.