哈喽,哈喽~,一年一度的抢火车票大战正式拉开序幕…
然饿大多数人碰到的是这种情况:当你满心期待摩拳擦掌准备抢票的时候,你会发现一票难求!想回趟家真难!
那么作为程序猿的你,当然要用程序猿的方式来抢票!下面分享用python来抢票!
欢迎关注公众号:每日治愈系列,提供各种免费资源!也可以咨询代码问题哦!
城市cookie可根据具体需求自行添加!不懂的点这里!
- 导入所需模块:
import re
from splinter.browser import Browser
from time import sleep
import sys
import httplib2
from urllib import parse
import smtplib
from email.mime.text import MIMEText
import time
此部分导入了代码所需的各种模块,用于实现不同的功能,包括正则表达式、网页浏览、时间操作、系统相关操作、HTTP连接、URL解析、发送电子邮件和短信等功能。
- BrushTicket 类初始化:
class BrushTicket(object):def __init__(self, passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile, receiver_email):# 初始化实例属性# ...
这部分定义了 BrushTicket
类并初始化了基于提供的参数的实例属性。这些属性包括乘客详细信息、车站信息、出发时间、座位类型、通知联系人和URL等。
- 登录功能:
def do_login(self):self.driver.visit(self.login_url)sleep(1)print('请扫描二维码登录或使用您的账号登录...')while True:if self.driver.url != self.init_my_url:sleep(1)else:break
do_login
方法负责登录网站。它打开登录的URL,等待用户手动登录(可以通过扫描二维码或使用账号登录),并等待登录成功。
- 车票预订功能:
def start_brush(self):self.driver.driver.maximize_window()self.do_login()self.driver.visit(self.ticket_url)try:# ...except Exception as error_info:print(error_info)
start_brush
方法实现了车票预订功能。它最大化浏览器窗口,执行登录操作,然后导航到车票预订页面。它不断查询是否有可用车票,选择所需的座位类型,并尝试为指定的乘客预订车票。它还处理异常情况,并在成功预订时提供通知。
- 短信通知:
def send_sms(self, mobile, sms_info):# ...
send_sms
方法负责发送短信通知。它使用"互亿无线"服务发送测试短信。它接收接收者的手机号码和短信内容作为参数,并返回从服务端收到的响应。
大家都有所体会12306那种神级验证码,即使是聪明如你也不定每次都能把验证码找对找全。然而经过训练后的模型却是可以的,不得不感叹科技强大。当然该程序中并没有涉及到这一识别验证码的核心技术,它只是将这一工作丢给第三方若快平台去做,然后自身利用结果即可。那么若快是如何识别这种神级验证码呢?通过机器学习 训练所需要的模型?笔者在这方面所了解的较少,不能给过多解释。但是这确实是个值得研究的方向。
- 邮件通知:
def send_mail(self, receiver_address, content):# ...
send_mail
方法处理发送电子邮件通知的功能。它使用SMTP协议连接到电子邮件服务器,使用发件人的凭据登录,并向指定的接收者地址发送电子邮件。它包括主题、正文以及发件人/收件人的详细信息。它接收接收者的电子邮件地址和邮件内容作为参数。
注:给我们推送相关消息,需要用户去提供账户和密码,这是个冒险的尝试,避免不了信息泄露的可能。
- 主程序执行:
if __name__ == '__main__':# 用户输入和初始化# ...# 开始预订车票ticket = BrushTicket(passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile, receiver_email)ticket.start_brush()
主程序执行部分接收用户输入以获取预订车票所需的各个参数,使用提供的值初始化 BrushTicket
对象,并调用 start_brush
方法开始预订车票的过程。
注意!!!以下是Python完整代码:
欢迎前往公众号:每日治愈系列,获取更多资源!
import re
from splinter.browser import Browser
from time import sleep
import sys
import httplib2
from urllib import parse
import smtplib
from email.mime.text import MIMEText
import timeclass BrushTicket(object):"""买票类及实现方法"""def __init__(self, passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile,receiver_email):"""定义实例属性,初始化"""# 乘客姓名self.passengers = passengers# 起始站和终点站self.from_station = from_stationself.to_station = to_station# 乘车日期self.from_time = from_time# 车次编号self.number = number.capitalize()# 座位类型所在td位置if seat_type == '商务座特等座':seat_type_index = 1seat_type_value = 9elif seat_type == '一等座':seat_type_index = 2seat_type_value = 'M'elif seat_type == '二等座':seat_type_index = 3seat_type_value = 0elif seat_type == '高级软卧':seat_type_index = 4seat_type_value = 6elif seat_type == '软卧':seat_type_index = 5seat_type_value = 4elif seat_type == '动卧':seat_type_index = 6seat_type_value = 'F'elif seat_type == '硬卧':seat_type_index = 7seat_type_value = 3elif seat_type == '软座':seat_type_index = 8seat_type_value = 2elif seat_type == '硬座':seat_type_index = 9seat_type_value = 1elif seat_type == '无座':seat_type_index = 10seat_type_value = 1elif seat_type == '其他':seat_type_index = 11seat_type_value = 1else:seat_type_index = 7seat_type_value = 3self.seat_type_index = seat_type_indexself.seat_type_value = seat_type_value# 通知信息self.receiver_mobile = receiver_mobileself.receiver_email = receiver_email# 新版12306官网主要页面网址self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html'self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'# 浏览器驱动信息,驱动下载页:https://sites.google.com/a/chromium.org/chromedriver/downloadsself.driver_name = 'chrome'self.driver = Browser(driver_name=self.driver_name)def do_login(self):"""登录功能实现,手动识别验证码进行登录"""self.driver.visit(self.login_url)sleep(1)# 选择登陆方式登陆print('请扫码登陆或者账号登陆……')while True:if self.driver.url != self.init_my_url:sleep(1)else:breakdef start_brush(self):"""买票功能实现"""# 浏览器窗口最大化self.driver.driver.maximize_window()# 登陆self.do_login()# 跳转到抢票页面self.driver.visit(self.ticket_url)try: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 = 0while self.driver.url == self.ticket_url:try:self.driver.find_by_text('查询').click()except Exception as error_info:print(error_info)sleep(1)continuesleep(0.2)count += 1local_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())print('第%d次点击查询……[%s]' % (count, local_date))try:current_tr = self.driver.find_by_xpath('//tr[@datatran="' + self.number + '"]/preceding-sibling::tr[1]')if current_tr:if current_tr.find_by_tag('td')[self.seat_type_index].text == '--':print('无此座位类型出售,已结束当前刷票,请重新开启!')sys.exit(1)elif current_tr.find_by_tag('td')[self.seat_type_index].text == '无':print('无票,继续尝试……')sleep(1)else:# 有票,尝试预订print('刷到票了(余票数:' + 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 = 1for p in self.passengers:if '()' in p:p = p[:-1] + '学生' + p[-1:]# 选择用户print('开始选择用户……')self.driver.find_by_text(p).last.click()# 选择座位类型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 += 1sleep(0.2)if p[-1] == ')':self.driver.find_by_id('dialog_xsertcj_ok').click()print('正在提交订单……')self.driver.find_by_id('submitOrder_id').click()sleep(2)# 查看放回结果是否正常submit_false_info = self.driver.find_by_id('orderResultInfo_id')[0].textif 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)continueprint('正在确认订单……')self.driver.find_by_id('qr_submit_id').click()print('预订成功,请及时前往支付……')# 发送通知信息self.send_mail(self.receiver_email, '恭喜您,抢到票了,请及时前往12306支付订单!')self.send_sms(self.receiver_mobile, '您的验证码是:1230。请不要把验证码泄露给其他人。')else:print('不存在当前车次【%s】,已结束当前刷票,请重新开启!' % self.number)sys.exit(1)except Exception as error_info:print(error_info)# 跳转到抢票页面self.driver.visit(self.ticket_url)except Exception as error_info:print(error_info)def send_sms(self, mobile, sms_info):"""发送手机通知短信,用的是-互亿无线-的测试短信"""host = "106.ihuyi.com"sms_send_uri = "/webservice/sms.php?method=Submit"account = "C59782899"pass_word = "19d4d9c0796532c7328e8b82e2812655"params = parse.urlencode({'account': account, 'password': pass_word, 'content': sms_info, 'mobile': mobile, 'format': 'json'})headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}conn = httplib2.HTTPConnectionWithTimeout(host, port=80, timeout=30)conn.request("POST", sms_send_uri, params, headers)response = conn.getresponse()response_str = response.read()conn.close()return response_strdef send_mail(self, receiver_address, content):"""发送邮件通知"""# 连接邮箱服务器信息host = 'smtp.163.com'port = 25sender = 'gxcuizy@163.com' # 你的发件邮箱号码pwd = '******' # 不是登陆密码,是客户端授权密码# 发件信息receiver = receiver_addressbody = '<h2>温馨提醒:</h2><p>' + content + '</p>'msg = MIMEText(body, 'html', _charset="utf-8")msg['subject'] = '抢票成功通知!'msg['from'] = sendermsg['to'] = receivers = smtplib.SMTP(host, port)# 开始登陆邮箱,并发送邮件s.login(sender, pwd)s.sendmail(sender, receiver, msg.as_string())if __name__ == '__main__':# 乘客姓名passengers_input = input('请输入乘车人姓名,多人用英文逗号“,”连接,(例如单人“张三”或者多人“张三,李四”,如果学生的话输入“王五()”):')passengers = passengers_input.split(",")while passengers_input == '' or len(passengers) > 4:print('乘车人最少1位,最多4位!')passengers_input = input('请重新输入乘车人姓名,多人用英文逗号“,”连接,(例如单人“张三”或者多人“张三,李四”):')passengers = passengers_input.split(",")# 乘车日期from_time = input('请输入乘车日期(例如“2018-08-08”):')date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')while from_time == '' or re.findall(date_pattern, from_time) == []:from_time = input('乘车日期不能为空或者时间格式不正确,请重新输入:')# 城市cookie字典city_list = {'lz':'%u5170%u5DDE%2CLZJ',#兰州'zz':'%u90D1%u5DDE%2CZZF',#郑州'pds':'%u5E73%u9876%u5C71%2CPEN',#平顶山'wh':'%u829C%u6E56%2CWHH',#芜湖}# 出发站from_input = input('请输入出发站,只需要输入首字母就行:')while from_input not in city_list.keys():from_input = input('出发站不能为空或不支持当前出发站(如有需要,请联系管理员!),请重新输入:')from_station = city_list[from_input]# 终点站to_input = input('请输入终点站,只需要输入首字母就行:')while to_input not in city_list.keys():to_input = input('终点站不能为空或不支持当前终点站(如有需要,请联系管理员!),请重新输入:')to_station = city_list[to_input]# 车次编号number = input('请输入车次号(例如“K1438”):')while number == '':number = input('车次号不能为空,请重新输入:')# 座位类型seat_type = input('请输入座位类型(例如“软卧”):')while seat_type == '':seat_type = input('座位类型不能为空,请重新输入:')# 抢票成功,通知该手机号码receiver_mobile = input('请预留一个手机号码,方便抢到票后进行通知(例如:18888888888):')mobile_pattern = re.compile(r'^1{1}\d{10}$')while receiver_mobile == '' or re.findall(mobile_pattern, receiver_mobile) == []:receiver_mobile = input('预留手机号码不能为空或者格式不正确,请重新输入:')receiver_email = input('请预留一个邮箱,方便抢到票后进行通知(例如:test@163.com):')while receiver_email == '':receiver_email = input('预留邮箱不能为空,请重新输入:')# 开始抢票ticket = BrushTicket(passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile,receiver_email)ticket.start_brush()
如若有各种问题,欢迎留言讨论!