香港云主机最佳企业级服务商!

ADSL拨号VPS包含了中国大陆(联通,移动,电信,)

中国香港,国外拨号VPS。

当前位置:云主机 > python >

电信ADSL拨号VPS
联通ADSL拨号VPS
移动ADSL拨号VPS

Python模拟登录12306的方法


时间:2021-11-09 10:26 作者:admin610456


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

(责任编辑:admin)






帮助中心
会员注册
找回密码
新闻中心
快捷通道
域名登录面板
虚机登录面板
云主机登录面板
关于我们
关于我们
联系我们
联系方式

售前咨询:17830004266(重庆移动)

企业QQ:383546523

《中华人民共和国工业和信息化部》 编号:ICP备00012341号

Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究

云官方微信

在线客服

  • 企业QQ: 点击这里给我发消息
  • 技术支持:383546523

  • 公司总台电话:17830004266(重庆移动)
  • 售前咨询热线:17830004266(重庆移动)