Python simulates login 12306, and python simulates login 12306
The example in this article describes how to simulate Python login 12306. Share it with you for your reference.
The specific implementation method is as follows:
Copy codeThe Code is 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, cookie 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/100 ')
Request. add_header ('Referer', 'https: // kyf201712306.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: // kyf201712306.cn ');
Req. add_header ('Referer', 'https: // kyf201712306.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/100 ')
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 Python programming.