1 '''
2 在学习过程中有什么不懂得可以加我的3 python学习交流扣扣qun,9341091704 群里有不错的学习视频教程、开发工具与电子书籍。5 与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容。6 '''
7
8 @@ -0,0 +1,124@@9 #-*- coding: utf-8 -*-
10 """
11 @author: liuyw12 """
13 from splinter.browser importBrowser14 from time importsleep15 importtraceback16 importtime, sys17
18 classhuoche(object):19 driver_name = ''
20 executable_path = ''
21 #用户名,密码
22 username = u"xxx"
23 passwd = u"xxx"
24 #cookies值得自己去找, 下面两个分别是沈阳, 哈尔滨
25 starts = u"%u6C88%u9633%2CSYT"
26 ends = u"%u54C8%u5C14%u6EE8%2CHBB"
27
28 #时间格式2018-01-19
29 dtime = u"2018-01-19"
30 #车次,选择第几趟,0则从上之下依次点击
31 order =032 ###乘客名
33 users = [u"xxx",u"xxx"]34 ##席位
35 xb = u"二等座"
36 pz = u"成人票"
37
38 """网址"""
39 ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
40 login_url = "https://kyfw.12306.cn/otn/login/init"
41 initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306"
42 buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc"
43
44 def __init__(self):45 self.driver_name = 'chrome'
46 self.executable_path = 'D:/chromedriver'
47
48 deflogin(self):49 self.driver.visit(self.login_url)50 self.driver.fill("loginUserDTO.user_name", self.username)51 #sleep(1)
52 self.driver.fill("userDTO.password", self.passwd)53 print(u"等待验证码,自行输入...")54 whileTrue:55 if self.driver.url !=self.initmy_url:56 sleep(1)57 else:58 break
59
60 defstart(self):61 self.driver = Browser(driver_name=self.driver_name,executable_path=self.executable_path)62 self.driver.driver.set_window_size(1400, 1000)63 self.login()64 #sleep(1)
65 self.driver.visit(self.ticket_url)66 try:67 print(u"购票页面开始...")68 #sleep(1)
69 #加载查询信息
70 self.driver.cookies.add({"_jc_save_fromStation": self.starts})71 self.driver.cookies.add({"_jc_save_toStation": self.ends})72 self.driver.cookies.add({"_jc_save_fromDate": self.dtime})73
74 self.driver.reload()75
76 count =077 if self.order !=0:78 while self.driver.url ==self.ticket_url:79 self.driver.find_by_text(u"查询").click()80 count += 1
81 print(u"循环点击查询... 第 %s 次" %count)82 #sleep(1)
83 try:84 self.driver.find_by_text(u"预订")[self.order - 1].click()85 exceptException as e:86 print(e)87 print(u"还没开始预订")88 continue
89 else:90 while self.driver.url ==self.ticket_url:91 self.driver.find_by_text(u"查询").click()92 count += 1
93 print(u"循环点击查询... 第 %s 次" %count)94 #sleep(0.8)
95 try:96 for i in self.driver.find_by_text(u"预订"):97 i.click()98 sleep(1)99 exceptException as e:100 print(e)101 print(u"还没开始预订 %s" %count)102 continue
103 print(u"开始预订...")104 #sleep(3)
105 #self.driver.reload()
106 sleep(1)107 print(u'开始选择用户...')108 for user inself.users:109 self.driver.find_by_text(user).last.click()110
111 print(u"提交订单...")112 sleep(1)113 self.driver.find_by_text(self.pz).click()114 self.driver.find_by_id('').select(self.pz)115 #sleep(1)
116 self.driver.find_by_text(self.xb).click()117 sleep(1)118 self.driver.find_by_id('submitOrder_id').click()119 print(u"开始选座...")120 self.driver.find_by_id('1D').last.click()121 self.driver.find_by_id('1F').last.click()122
123 sleep(1.5)124 print(u"确认选座...")125 self.driver.find_by_id('qr_submit_id').click()126
127 exceptException as e:128 print(e)129
130 if __name__ == '__main__':131 huoche =huoche()132 huoche.start()