Python火车票代码

前言:我想在源文章里加入下面一段来着,但是实在加不上,报什么405,造成安全威胁,不好意思哈各位,这么点内容还另起一章实在不好意思哈。

    千万别一直挂着会封ip的,别问我怎么知道的,我不想说T_T


代码如下:

虽然还是存在很多的问题,但是真的不愿意改了,票买到了就没什么动力,大家懂得,上面的代码的确存在问题不好意思:

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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 04 00:35:24 2015
 
@author: Ye
"""
 
from  splinter.browser  import  Browser
from  time  import  sleep
 
#用户名,密码
username  =  "用户名"
passwd  =  "密码"
#cookies值得自己去找,下面两个分别是上海,长沙,怎么找blog中说明了
starts  =  "%u4E0A%u6D77%2CSHH"
ends  =  "%u957F%u6C99%2CCSQ"
#时间格式2016-01-31
dtime  =  "2016-02-02"
#车次,选择第几趟,0则从上之下依次点击
order  =  0
###乘客名
pa  =  u "乘客名(常用联系人)"
###车次类型
ttype = "GC-高铁/城际"
 
#网址
ticket_url  =  "https://kyfw.12306.cn/otn/leftTicket/init"
login_url  =  "https://kyfw.12306.cn/otn/login/init"
initmy_url  =  "https://kyfw.12306.cn/otn/index/initMy12306"
config_url  =  "https://kyfw.12306.cn/otn/confirmPassenger/initDc"
 
def  login():
b.find_by_text(u "登录" ).click()
sleep( 3 )
b.fill( "loginUserDTO.user_name" ,username)
sleep( 1 )
b.fill( "userDTO.password" ,passwd)
sleep( 1 )
b.execute_script( 'alert("自行输入验证码吧~")' )
print  u "等待验证码,自行输入..."
sleep( 10 )
 
def  huoche():
global  b
=  Browser(driver_name = "chrome" )
b.visit(ticket_url)
b.execute_script( 'alert("开始刷票喽~~~~")' )
sleep( 2 )
b.get_alert().dismiss()
 
while  b.is_text_present(u "登录" ):
sleep( 1 )
login()
if  b.url  = =  initmy_url:
break
 
try :
#跳回购票页面
b.visit(ticket_url)
 
#加载查询信息
b.cookies.add({ "_jc_save_fromStation" :starts})
b.cookies.add({ "_jc_save_toStation" :ends})
b.cookies.add({ "_jc_save_fromDate" :dtime})
b. reload ()
=  1
 
#循环点击预订
if  order ! =  0 :
while  b.url  = =  ticket_url:
b.find_by_text(u "查询" ).click()
#                b.find_by_text(ttype).click()
 
if  b.find_by_text(u "预订" ):
sleep( 0.3 )
b.find_by_text(u "预订" )[order  -  1 ].click()
print  b.url
 
if  b.is_text_present(u "证件号码" ,wait_time = 0.2 ):
#                        print [ i.text for i in b.find_by_text(pa) ]
b.find_by_text(pa)[ 1 ].click()
 
else :
b.execute_script( 'alert("似乎没有可预订选项")' )
b.get_alert().dismiss()
pass
 
else :
while  b.url  = =  ticket_url:
b.find_by_text(u "查询" ).click()
if  b.find_by_text(u "预订" ):
sleep( 0.3 )
for  in  b.find_by_text(u "预订" ):
i.click()
sleep( 0.1 )
if  b.is_text_present(u "证件号码" ):
b.find_by_text(pa)[ 1 ].click()
 
else :
b.execute_script( 'alert("似乎没有可预订选项")' )
b.get_alert().dismiss()
pass
 
b.execute_script( 'alert("能做的都做了")' )
b.get_alert().dismiss()
 
print   u "能做的都做了.....不再对浏览器进行任何操作"
 
except  Exception:
print  u "出错了...."
 
if  __name__  = =  "__main__" :
huoche()

放上来怎么没有缩进了T_T

好吧,还是百度云吧

http://pan.baidu.com/s/1gdTu7cR


本文转自 youerning 51CTO博客,原文链接:http://blog.51cto.com/youerning/1719911


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值