欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python模拟登录12306的方法

程序员文章站 2022-06-20 23:46:15
本文实例讲述了python模拟登录12306的方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下: #!/usr/bin/python # -*-...

本文实例讲述了python模拟登录12306的方法。分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:

#!/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/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();

希望本文所述对大家的python程序设计有所帮助。