The example in this article describes how Python simulates login 12306. Share to everyone for your reference.
The implementation methods are as follows:
Copy Code code as follows:
#!/usr/bin/python
#-*-Coding:utf-8-*-
import re;
Import SYS;
Import Cookielib;
Import Urllib;
Import Urllib2;
Import Optparse;
Import JSON;
Import Httplib2;
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ');
Def Login ():
CJ = Cookielib. Cookiejar ();
Opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ));
Urllib2.install_opener (opener);
Print "--------------[Step1] to get Cookie";
URL = "Https://kyfw.12306.cn/otn/login/init";
RESP = Urllib2.urlopen (URL);
For index, cookies in Enumerate (CJ):
print ' [', Index, '] ', cookie;
Print "--------------[Step2] to get Code";
URL2 = "Https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand";
RESP2 = Urllib2.urlopen (URL2);
#respInfo2 = Resp2.info ();
#print "respinfo=", RespInfo2;
With open ("Code.png", "WB") as Image:
Image.write (Resp2.read ())
Codestr = Sys.stdin.readline ();
CODESTR = Codestr[:-1]
Print "--------------[step3] to check code";
Ajax_url = "Https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn";
DC = {
' Randcode ': codestr,
' Rand ': ' Sjrand '
};
Request = Urllib2. Request (Ajax_url, Urllib.urlencode (DC))
Request.add_header ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ")
Request.add_header (' X-requested-with ', ' XMLHttpRequest ')
Request.add_header (' user-agent ', ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/ 33.0.1750.154 safari/537.36 ')
Request.add_header (' Referer ', ' https://kyfw.12306.cn/otn/login/init ')
Request.add_header (' Accept ', ' */* ')
Request.add_header (' accept-encoding ', ' gzip, deflate ')
f = urllib2.urlopen (Request)
Print (F.read ())
Print "--------------[STEP4] to login";
loginurl = "Http://kyfw.12306.cn/otn/login/loginAysnSuggest";
DC = {
' Randcode ': codestr,
' Userdto.password ': "sunyuke1989",
' Loginuserdto.user_name ': "sunyuke@qq.com"
};
req = Urllib2. Request (loginurl, Urllib.urlencode (DC));
Req.add_header (' Content-type ', "application/x-www-form-urlencoded");
Req.add_header (' X-requested-with ', ' XMLHttpRequest ');
Req.add_header (' Origin ', ' https://kyfw.12306.cn ');
Req.add_header (' Referer ', ' https://kyfw.12306.cn/otn/login/init ');
Req.add_header (' Accept ', ' */* ');
Req.add_header (' accept-encoding ', ' gzip, deflate ');
Req.add_header (' Connection ', ' keep-live ');
Request.add_header (' user-agent ', ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/ 33.0.1750.154 safari/537.36 ')
RESP = Urllib2.urlopen (req);
Print (Resp.read (). Encode (' GB18030 '));
Loginingurl = "Https://kyfw.12306.cn/otn/login/userLogin";
req = Urllib2. Request (Loginingurl, "");
Print "--------------[step5] to Queryuserinfo";
Loginingurl = "Https://kyfw.12306.cn/otn/modifyUser/initQueryUserInfo";
req = Urllib2. Request (Loginingurl, "");
RESP = Urllib2.urlopen (req);
info = Resp.read ();
Print (Resp.read (). Encode (' GB18030 '));
If __name__== "__main__":
Login ();
I hope this article will help you with your Python programming.