前段时间在GitHub上看到一段刷票的脚步代码,下下来稍稍改了下加了个简陋的UI界面,另外把车站列表等信息弄成了csv文件,方便不改源码的情况下添加车站。
先上核心内容:
- try:
- if hasattr(print_func, '__call__'):
- print_func('开始刷票……')
- # print('开始刷票……')
- # 加载车票查询信息
- self.driver.cookies.add({"_jc_save_fromStation": self.from_station})
- self.driver.cookies.add({"_jc_save_toStation": self.to_station})
- self.driver.cookies.add({"_jc_save_fromDate": self.from_time})
- self.driver.reload()
- count = 0
- while self.driver.url == self.ticket_url:
- try:
- self.driver.find_by_text('查询').click()
- except Exception as error_info:
- print(error_info)
- sleep(1)
- continue
- sleep(0.5 * refresh_times)
- count += 1
- local_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- if hasattr(print_func, '__call__'):
- print_func('第%d次点击查询……[%s]' % (count, local_date))
- # print('第%d次点击查询……[%s]' % (count, local_date))
- try:
- for car_no in self.numbers:
- current_tr = self.driver.find_by_xpath('//tr[@datatran="' + car_no + '"]/preceding-sibling::tr[1]')
- if current_tr:
- current_time = current_tr.find_by_css('.start-t').text
- if current_tr.find_by_tag('td')[self.seat_type_index].text == '--':
- if hasattr(print_func, '__call__'):
- print_func('%s无此座位类型出售……' % (car_no + '(' + current_time + ')',))
- # print('%s无此座位类型出售……' % (car_no + '(' + current_time + ')',))
- sleep(0.2)
- elif current_tr.find_by_tag('td')[self.seat_type_index].text == '无':
- if hasattr(print_func, '__call__'):
- print_func('%s无票……' % (car_no + '(' + current_time + ')',))
- # print('%s无票……' % (car_no + '(' + current_time + ')',))
- sleep(0.2)
- else:
- # 有票,尝试预订
- if hasattr(print_func, '__call__'):
- print_func(car_no + '(' + current_time + ')刷到票了(余票数:' + str(
- current_tr.find_by_tag('td')[self.seat_type_index].text) + '),开始尝试预订……')
- # print(car_no + '(' + current_time + ')刷到票了(余票数:' + str(
- # current_tr.find_by_tag('td')[self.seat_type_index].text) + '),开始尝试预订……')
- current_tr.find_by_css('td.no-br>a')[0].click()
- sleep(1)
- key_value = 1
- for p in self.passengers:
- if '()' in p:
- p = p[:-1] + '学生' + p[-1:]
- # 选择用户
- if hasattr(print_func, '__call__'):
- print_func('开始选择用户……')
- # print('开始选择用户……')
- self.driver.find_by_text(p).last.click()
- # 选择座位类型
- if hasattr(print_func, '__call__'):
- print_func('开始选择席别……')
- # print('开始选择席别……')
- if self.seat_type_value != 0:
- self.driver.find_by_xpath(
- "//select[@id='seatType_" + str(key_value) + "']/option[@value='" + str(
- self.seat_type_value) + "']").first.click()
- key_value += 1
- sleep(0.5)
- if p[-1] == ')':
- self.driver.find_by_id('dialog_xsertcj_ok').click()
- sleep(0.1)
- if hasattr(print_func, '__call__'):
- print_func('正在提交订单……')
- # print('正在提交订单……')
- self.driver.find_by_id('submitOrder_id').click()
- sleep(1)
- # 查看放回结果是否正常
- submit_false_info = self.driver.find_by_id('orderResultInfo_id')[0].text
- if submit_false_info != '':
- print(submit_false_info)
- self.driver.find_by_id('qr_closeTranforDialog_id').click()
- sleep(0.2)
- self.driver.find_by_id('preStep_id').click()
- sleep(0.3)
- continue
- if hasattr(print_func, '__call__'):
- print_func('正在确认订单……')
- # print('正在确认订单……')
- sleep(1)
- self.driver.find_by_id('qr_submit_id').click()
- if hasattr(print_func, '__call__'):
- print_func('预订成功,请及时前往支付……')
- # print('预订成功,请及时前往支付……')
- # 发送通知信息
- self.send_mail(self.receiver_email, '恭喜您,抢到票了,请及时前往12306支付订单!')
- # self.send_sms(self.receiver_mobile, '您的验证码是:8888。请不要把验证码泄露给其他人。')
- else:
- if hasattr(print_func, '__call__'):
- print_func('当前车次异常')
- # print('当前车次异常')
- except Exception as error_info:
- if hasattr(print_func, '__call__'):
- print_func(error_info)
- # print(error_info)
- # 跳转到抢票页面
- self.driver.visit(self.ticket_url)
- except Exception as error_info:
- print(error_info)
其中self.driver是需要引入splinter.browser.Browser,根据源码可以看到支持的浏览器类型基本覆盖了常用的浏览器了。
_DRIVERS = {
'chrome': None,
'edge': None,
'firefox': None,
'remote': None,
'django': None,
'flask': None,
'zope.testbrowser': None,
}
我用的是Chrome浏览器,以此为例需要先查看自身浏览器的版本号下载对应版本的驱动。

由于墙的存在下载速度太慢,可以选择国内源:http://npm.taobao.org/mirrors/chromedriver/

下载后将压缩包里的文件复制到Chrome浏览器安装目录的Chrome/Application目录下。
以上步骤都弄好后可以先在python console尝试一下 ,看是否成功调出Chrome浏览器。

- from splinter.browser import Browser
- driver = Browser(driver_name='chrome')
原脚本文件运行后是在黑窗窗上输入个类信息的,我加了个ui界面,输入相对方便些,但是要用于购票的话,还是需要提前上12306网站查看车次信息。因为这里并没有加爬取网页车次信息和座位类型供选择的功能,若有感兴趣的欢迎下载源码修改完善。https://gitee.com/solun/python-pro.git

当然,如果有需要也可以直接下载我打包好的exe文件直接使用,只是每次购票的时候都需要重复扫码登录。另外车站列表中只有部分城市的火车站,若要添加新的可按如下方法操作。
下载链接
链接:12306购票辅助工具
提取码:4tfj
--来自百度网盘超级会员V5的分享

如图所示,在浏览器中打开12306网址,并选择车站名,打开开发者模式,选择“网络”,点击任意一条请求查看cookie内容, 其中_jc_save_fromStation= %u6C88%u9633%2CSYT 代表刚刚选择的出发站沈阳站; _jc_save_toStation= %u4E0A%u6D77%2CSHH 代表目的地上海站; 将加粗部分的编码复制到stations.csv中,按格式编辑即可。