python模拟登陆12306

sun6623518 2014-04-21 04:04:24
请教各位高手:
我尝试用下面代码去模拟登陆12306,验证码下载成功,验证也成功。
在第四步登陆http://kyfw.12306.cn/otn/login/loginAysnSuggest的时候
为啥最后返回结果总是,“网络忙,请刷新重试”


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

...全文
1455 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_32015613 2015-12-20
  • 打赏
  • 举报
回复
大神 手动输入验证码的时候 该怎么输入 输入的是数字吗
ling0xi1 2015-01-28
  • 打赏
  • 举报
回复
因为安装的是python3.2版本,所以改造了一下,但是发现还是有问题,提示网络忙,关闭插件重试。可能12306反爬升级了吧。这里有个py2的例子,不过比较新,看了看貌似要解析一段js,顺便提交到登录的data里面,可能原因在这。无奈没怎么找到解析js的模块在py3里面,有没有好的解决办法......(想把改过的代码文件附过来,没发现添加附件的地方啊)
语默静喧 2014-06-12
  • 打赏
  • 举报
回复
代码很好,不过同学,你还是把自己的12306密码修改下吧
sun6623518 2014-04-22
  • 打赏
  • 举报
回复
引用 1 楼 iasky 的回复:
是不是被封IP了?还是他检测到你不熟正常的浏览器登录。
没有被封呢,我浏览器用12306正常 我在User-Agent字段已经伪装成浏览器了
iasky 2014-04-22
  • 打赏
  • 举报
回复
是不是被封IP了?还是他检测到你不熟正常的浏览器登录。
sun6623518 2014-04-22
  • 打赏
  • 举报
回复
不好意思是我的疏忽,第61行 LoginUrl = "http://kyfw.12306.cn/otn/login/loginAysnSuggest"; 应该写为https的 LoginUrl = "https://kyfw.12306.cn/otn/login/loginAysnSuggest"; 这就结贴。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧