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

原帖来自 http://bbs.csdn.net/topics/390765711

最新文章

  1. Linux初识二
  2. 【转】Hive内部表、外部表
  3. linux文件基本属性
  4. Python2.3-原理之语句和语法
  5. 第七章 springboot + retrofit
  6. linux下对sh文件的操作
  7. 使用DataList 分页方法
  8. telnet如何操作Memcached缓存系统?
  9. Chromium如何显示Web页面
  10. 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。
  11. Windows进程间通信(上)
  12. C++如何返回不定长数组
  13. 谁能用通俗的语言解释一下什么是 RPC 框架?
  14. [转]python中pandas库中DataFrame对行和列的操作使用方法
  15. JavaWeb——jsp-config
  16. JSP与Servlet之间传值的一般方法,适合新手做练习
  17. poj2135 Farm Tour(费用流)
  18. Windows下使用最新的JDK1.7.0_51以上版本连接Jenkins出现SecurityException
  19. 删除XML文件中的空格
  20. [转载]AMD 的 CommonJS wrapping

热门文章

  1. HDU [P5015] 233 Matrix
  2. ext4向后兼容代码
  3. Python_代码练习_写一个判断是否为小数的函数
  4. C# 加载图片image --(C#)Image.FromFile 方法会锁住文件的原因及可能的解决方法
  5. 爬虫学习笔记(三)requests模块使用
  6. (18) python 爬虫实战
  7. google搜索打不开?提供 国内几个给力的服务器
  8. Zabbix4.0安装与入门及常见配置
  9. python 集合互相转换
  10. Install Ruby on Rails on Ubuntu 12.04 LTS