12306订票代码--python订票的另类方式

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
'''
Created on 2014年12月30日
 
@author: liujichao
'''
import sys
import logging
from splinter import Browser
import time
logging.getLogger().setLevel(logging.ERROR)
reload(sys)
sys.setdefaultencoding('utf-8'# @UndefinedVariable
browser = Browser()
def checkTick():
    while True:
        if int(time.strftime("%H", time.localtime())) in [23,0,1,2,3,4,5,6]:
            print "23点到7点不能登录"
            time.sleep(200)
            continue
            pass
        break
        pass
    browser.visit("https://kyfw.12306.cn/otn/login/init")
    browser.find_by_id("username").fill("用户名")
    browser.find_by_id("password").fill("密码")
    browser.find_by_id("randCode").click()
     
    while True:       
        while len(browser.find_by_id("randCode").value)<4 or not browser.find_by_id("i-okmypasscode1").visible:
            time.sleep(1)
            pass
        browser.find_by_id("loginSub").click()
        warnAlert=browser.find_by_id("content_defaultwarningAlert_id")
        if  len(warnAlert)!=0 and warnAlert.visible:
            browser.find_by_id("qd_closeDefaultWarningWindowDialog_id").click()
            browser.execute_script('''refreshImg('login','sjrand');''')
            browser.find_by_id("randCode").click()
            continue
            pass
        break
        pass
    while browser.url !="https://kyfw.12306.cn/otn/index/initMy12306":
        print browser.url
        time.sleep(1)
        pass   
    browser.visit('https://kyfw.12306.cn/otn/leftTicket/init')
    #browser.fill('kw', 'splinter - python acceptance testing for web applications')
    #browser.execute_script("$('body').empty()")
    #print browser.find_by_id("login_user")
    #browser.find_by_id("login_user").click()
    #print browser.cookies()
     
    browser.find_by_id("fromStationText").click()
    browser.execute_script('''$(\"#ul_list1 > li:nth-child(1)\").click();
    ''')
     
    browser.find_by_id("toStationText").click()
    browser.execute_script('''
    $(\"#nav_list3\").click();
    $(\"a.cityflip:nth-child(1)\").click();
    $(\"#ul_list3 > li:nth-child(21)\").click();
    ''')
     
     
    browser.find_by_value("G").check()
    #browser.find_by_id("train_date").click()
    #time.sleep(10)
    browser.execute_script('''
    setInterval(function(){
    $.ajax({
            type: 'post',
            url: '/otn/login/checkUser',
            data: {
            },
            beforeSend: function (c) {
                c.setRequestHeader('If-Modified-Since', '0');
                c.setRequestHeader('Cache-Control', 'no-cache')
            },
            success: function (e) {
                if (!e.data.flag) {
                   location.href="https://kyfw.12306.cn/otn/login/init";
                }
            }
        });
},2000);
    ''')
    while True:
        if browser.url=="https://kyfw.12306.cn/otn/login/init":
            #被踢了
            print "被踢了"
            raise Exception("被踢")
            pass
        browser.execute_script('''$("#train_date").val("2015-02-18");''')
        browser.find_by_id("query_ticket").click()
        browser.execute_script("CLeftTicketUrl = 'leftTicket/queryT?_r="+str(time.time())+"';")
        browser.execute_script('''$("#train_date").val("2015-02-18");''')
        warnAlert=browser.find_by_id("content_defaultwarningAlert_id")
        if  len(warnAlert)!=0:
            browser.find_by_id("qd_closeDefaultWarningWindowDialog_id").click()
            pass
        browser.execute_script('''
        iflagt=false;
$('#queryLeftTable tr').each(function () {
  trId = $(this).attr('id');
  if (/^ticket/.test(trId)) {
    tid=trId.replace("ticket_","");
    tranCode=$("#"+trId+"_train").find("a").text()
    if("G655,G6741,G67,G517,G491,".indexOf(tranCode+",")!=-1){
        //二等座位
        ZEtxt=$("#ZE_"+tid).text();
        zcount=0;
        try
        {
         zcount=parseInt(ZEtxt)
        } catch (e) {zcount=0;}
      console.log(ZEtxt);
      txtInfo=$("#"+trId+">.no-br").text()
      if((ZEtxt=="有" || zcount>1) && txtInfo!="23:00-07:00系统维护时间"){
        $(this).find(".btn72").click();
        iflagt=true;
        return false;
      }
    }
     
  }
});
 
''')
         
        if browser.evaluate_script("iflagt;"):
            break
            pass
        else:
            print "无"
            pass
        time.sleep(3)
        pass
    while browser.url !="https://kyfw.12306.cn/otn/confirmPassenger/initDc":
        print browser.url
        time.sleep(1)
        pass  
    browser.find_by_id("gd").click()
    browser.find_by_id("normalPassenger_0").check()
    browser.find_by_id("normalPassenger_24").check()
    browser.find_by_id("randCode").click()
     
    while len(browser.find_by_id("randCode").value)<4:
        time.sleep(1)
        pass
    browser.is_element_not_present_by_id(id, wait_time=None)
    while not browser.find_by_id("checkticketinfo_id").visible:
        time.sleep(0.5)
        pass
    browser.find_by_id("qr_submit_id").click()
    #browser.find_by_id("fromStation").fill("BXP")
    #browser.find_by_id("train_date").fill("BXP")
    #browser.find_by_id("query_ticket").click()
    #print browser.html
    print browser.cookies.all()
    pass
if __name__ == '__main__':
    while True:
        try:
            checkTick()
        except Exception,e:
            browser.quit()
            browser = Browser()
            print e
            pass
        pass
    pass