This is a simulated login crawler. After running to enter the user name password and the program in the same directory of the Verification code picture, the successful landing can show you in the watercress nickname.
#!/usr/bin/env python#!-*-coding:utf-8-*-ImportJSONImportUrllibImportUrllib2ImportCookielibclassZhihu_login (object):"""Login to a demo program""" def __init__(self): Self.test_url="Http://www.zhihu.com/topic"Self.login_url="Http://www.zhihu.com/login/email"Self.checkcode_url="Http://www.zhihu.com/captcha.gif" """create a opener with a cookie"""CJ=Cookielib. Cookiejar () Self.opener=Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ)); defGetcheckcode (self):"""get the verification code, saved in the current directory"""pic= Open ("Checkcode.gif","W") """The verification code must be opened with Self.opener, because the server needs to verify that the cookie matches the current submitted verification code, opener the request can be sent together with the cookie. """resp=Self.opener.open (Self.checkcode_url)Print>>pic, Resp.read () pic.close ()defLogin (self): Post_data={} post_data["Email"] = Raw_input ("Enter user name: \ n") post_data["Password"] = Raw_input ("Enter password: \ n") Self.getcheckcode () Info= Raw_input ("Enter the verification code, can't see the input '? ' Let's change another one \ n") while(Info = ='?'): Self.getcheckcode () Info= Raw_input ("Enter the verification code, can't see the input '? ' Let's change another one \ n") post_data["Captcha"] =Info Post_data=Urllib.urlencode (post_data) Resp=Self.opener.open (Self.login_url, post_data) status=json.loads (Resp.read ())ifstatus["R"] ==0:Print "Landing Success!" PrintJson.dumps (status, Ensure_ascii=false). Encode ("Utf-8","Ignore") #print "Start test ... " #self.test () Else: Print "Login failed!" Print "The failure information is as follows:" PrintJson.dumps (status, Ensure_ascii=false). Encode ("Utf-8","Ignore") defTest (self):"""See if you can find your name in the source code, if that means it's true. """resp=Self.opener.open (Self.test_url)PrintResp.read ()if __name__=="__main__": Zhihu_login (). Login ()
Acknowledgement: Chi Li
Python Demo Login Knowledge