Python模拟登录12306
2024-09-06 21:21:00
#!/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
最新文章
- Linux初识二
- 【转】Hive内部表、外部表
- linux文件基本属性
- Python2.3-原理之语句和语法
- 第七章 springboot + retrofit
- linux下对sh文件的操作
- 使用DataList 分页方法
- telnet如何操作Memcached缓存系统?
- Chromium如何显示Web页面
- 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。
- Windows进程间通信(上)
- C++如何返回不定长数组
- 谁能用通俗的语言解释一下什么是 RPC 框架?
- [转]python中pandas库中DataFrame对行和列的操作使用方法
- JavaWeb——jsp-config
- JSP与Servlet之间传值的一般方法,适合新手做练习
- poj2135 Farm Tour(费用流)
- Windows下使用最新的JDK1.7.0_51以上版本连接Jenkins出现SecurityException
- 删除XML文件中的空格
- [转载]AMD 的 CommonJS wrapping
热门文章
- HDU [P5015] 233 Matrix
- ext4向后兼容代码
- Python_代码练习_写一个判断是否为小数的函数
- C# 加载图片image --(C#)Image.FromFile 方法会锁住文件的原因及可能的解决方法
- 爬虫学习笔记(三)requests模块使用
- (18) python 爬虫实战
- google搜索打不开?提供 国内几个给力的服务器
- Zabbix4.0安装与入门及常见配置
- python 集合互相转换
- Install Ruby on Rails on Ubuntu 12.04 LTS