Python模拟登录12306的方法

作者:shichen2014 时间:2023-03-11 09:12:36 

本文实例讲述了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程序设计有所帮助。

标签:Python,模拟,登录
0
投稿

猜你喜欢

  • python双向链表实例详解

    2023-10-28 08:25:09
  • 使用setup.py安装python包和卸载python包的方法

    2023-01-31 02:20:47
  • Python Web框架Flask信号机制(signals)介绍

    2022-12-01 13:52:23
  • SQL SERVER数据操作类代码

    2024-01-24 02:20:47
  • keras和tensorflow使用fit_generator 批次训练操作

    2023-04-09 04:55:59
  • Trie树_字典树(字符串排序)简介及实现

    2022-02-22 23:18:23
  • python图形开发GUI库pyqt5的基本使用方法详解

    2022-07-08 02:31:55
  • 浅谈numpy数组的几种排序方式

    2022-04-24 12:48:15
  • pytest实现多进程与多线程运行超好用的插件

    2023-03-23 15:56:23
  • go doudou开发gRPC服务快速上手实现详解

    2024-05-05 09:33:11
  • Windows下的Python 3.6.1的下载与安装图文详解(适合32位和64位)

    2021-06-28 16:02:24
  • Python 列表与链表的区别详解

    2022-01-24 10:10:04
  • python实现简单ftp客户端的方法

    2023-12-14 20:07:31
  • sklearn+python:线性回归案例

    2023-10-19 20:07:01
  • Python 依赖库太多了该如何管理

    2021-06-22 22:29:02
  • Python如何爬取51cto数据并存入MySQL

    2024-01-15 02:18:16
  • 如何防止页面中的敏感信息被提取

    2008-05-04 11:59:00
  • 不同浏览器所支持的“事件”

    2007-09-26 18:29:00
  • JavaScript前端学算法题解LeetCode最大重复子字符串

    2024-05-21 10:21:00
  • 详解解Django 多对多表关系的三种创建方式

    2021-03-13 04:59:16
  • asp之家 网络编程 m.aspxhome.com