#!/usr/bin/env Python3#-*-coding:utf-8-*-" "required-requests (required)-pillow (optional) Info-author: "Xchaoinfo"-Email: "[email protected]"-Date: "2016.2.4" Updat E-name: "WANGMENGCN"-Email: "[email protected]"-Date: "2016.4.21"" "ImportRequestsTry: ImportCookielibexcept: ImportHttp.cookiejar as CookielibImportReImport TimeImportOs.pathTry: fromPILImportImageexcept: Pass#Construct Request HeadersAgent ='mozilla/5.0 (Windows NT 5.1; rv:33.0) gecko/20100101 firefox/33.0'Headers= { 'user-agent': Agent}#using login cookie informationSession =requests.session () session.cookies= Cookielib. Lwpcookiejar (filename='Cookies')Try: Session.cookies.load (Ignore_discard=True)except: Print("Cookie failed to load")defget_xsrf ():" "_XSRF is a dynamically changing parameter." "Index_url='http://www.zhihu.com' #get the _xsrf you need to log inIndex_page = Session.get (Index_url, headers=headers) HTML=Index_page.text Pattern= R'name= "_XSRF" value= "(. *?)"' #The _XSRF here returns a list_XSRF =Re.findall (pattern, HTML)return_xsrf[0]#Get Verification CodedefGet_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 ()#Display the verification code with the pillow Image #If you do not install pillow to the directory where the source code is located, locate the verification code and enter it manually Try: Im= Image.open ('captcha.jpg') Im.show () im.close ()except: Print(U'please go to the%s directory to find captcha.jpg manual input'% Os.path.abspath ('captcha.jpg')) Captcha= Input ("Please input the captcha\n>") returnCaptchadefIsLogin ():#determine if you are logged in by viewing the user's personal informationURL ="Https://www.zhihu.com/settings/profile"Login_code= Session.get (url,allow_redirects=False). Status_codeifint (x=login_code) = = 200: returnTrueElse: returnFalsedefLogin (Secret, account):#determine if the phone number is based on the user name entered ifRe.match (R"^1\d{10}$", account):Print("phone number login \ n") Post_url='Http://www.zhihu.com/login/phone_num'PostData= { '_XSRF': Get_xsrf (),'Password': Secret,'Remember_me':'true', 'Phone_num': Account,}Else: Print("Mailbox login \ n") Post_url='Http://www.zhihu.com/login/email'PostData= { '_XSRF': Get_xsrf (),'Password': Secret,'Remember_me':'true', 'Email': Account,}Try: #No verification code required for direct login SuccessLogin_page = Session.post (Post_url, Data=postdata, headers=headers) Login_code=Login_page.textPrint(Login_page.status)Print(Login_code)except: #You need to enter a verification code to log in successfullypostdata["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__': ifIsLogin ():Print('you are already logged in') Else: Account= Input ('Please enter your user name \n>') Secret= Input ("Please enter your password \n>") Login (Secret, account)
Python emulation login, including verification code (RPM)