Python emulation method for login 12306

Source: Internet
Author: User
This example describes the Python emulation method for login 12306. Share to everyone for your reference.

The implementation method is as follows:

Copy the 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.c Om "
};
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 ();

Hopefully this article will help you with Python programming.

  • 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.