Python Demo Login Knowledge

Source: Internet
Author: User

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

Related Article

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.