美文网首页Python学习大数据 爬虫Python AI Sql
Python黑科技,以最快速度抢到回家的火车票!

Python黑科技,以最快速度抢到回家的火车票!

作者: 1a076099f916 | 来源:发表于2019-01-13 14:36 被阅读7次

又是一年,马上就要回家过年了,还没有买到票的小伙伴们是否已经像热锅上的蚂蚁了无脑的开始找黄牛了?

Python黑科技,以最快速度抢到回家的火车票!

俗话说的好,求人不如求自己,抢票这玩意,其实我觉得我也可以~

Python黑科技,以最快速度抢到回家的火车票!

来来来,直接入正题吧!

这里有两种方案,一种是基于Selenium2的自动化框架控制浏览器实现,另一种方案则是基于Python自身的爬虫package如request,urllib等来实现,本文实现第一种方案,第二种方案也在摸索中。

实现过程

一、新版12306官网主要页面网址

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">https://kyfw.12306.cn/otn/resources/login.html
https://kyfw.12306.cn/otn/view/index.html
https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc
</pre>

二、浏览器驱动信息

这里使用的是chrome浏览器

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">在此处下载
https://sites.google.com/a/chromium.org/chromedriver/downloads
</pre>

三、初始化必要信息

买票必要信息,比如乘车人,来往车站,车次类型等等

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 乘客姓名
passengers_input = 'xxx'
passengers = passengers_input.split(",")

乘车日期,格式为2019-01-10

from_time = '2019-01-31'

城市cookie字典

city_list = {
'sz':'%u6DF1%u5733%2CSZQ', #深圳
'zz':'%u90D1%u5DDE%2CZZF', #郑州
'gz':'%u5E7F%u5DDE%2CGZQ', #广州
}

出发站

from_station = '%u5E7F%u5DDE%2CGZQ'

终点站

to_station = '%u90D1%u5DDE%2CZZF'

车次编号

numbers = ['Z90','Z190','Z14','Z168','Z98','T124','T370']

座位类型

seat_type = '硬卧'

通知邮件

receiver_email = 'xxxxxxxxx@163.com'
</pre>

注意:城市字典是将城市简写和城市代码之间联系起来。如果没有你想要到城市,可以在打开12306的查票页面,然后打开浏览器的开发者模式,输入对应的城市,然后查看请求信息。

Python黑科技,以最快速度抢到回家的火车票!

image.png

四、登录功能实现

登录我们需要手动登录,扫码或者输入账号都可以。登录后记录session,供后面抢票使用

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
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
</pre>

五、抢票功能实现

核心功能实现,通过扫描关键字来确认是否有余票,然后实现点击页面,进而走向下一步。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">current_tr = self.driver.find_by_xpath('//tr[@datatran="' + number + '"]/preceding-sibling::tr[1]')
if current_tr:
if current_tr.find_by_tag('td')[self.seat_type_index].text == '--':
print('无此座位类型出售,已结束当前刷票,请重新开启!')
sys.exit(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 = 1
for p in self.passengers:

选择用户

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 += 1
sleep(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)
print('正在确认订单……')
self.driver.find_by_id('qr_submit_id').click()
</pre>

六、抢票成功后邮件通知自己

一个很简单的功能,不过却是必要的,避免自己错过!

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def send_mail(self, receiver_address, content):
"""发送邮件通知"""

连接邮箱服务器信息

host = 'xxxx.163.com'
port = 25
sender = 'xxxxxx@163.com' # 你的发件邮箱号码
pwd = 'xxxxx' # 第三方授权登录吗,不是登录密码!!

发件信息

receiver = receiver_address
body = '<h2>温馨提醒:</h2><p>' + content + '</p>'
msg = MIMEText(body, 'html', _charset="utf-8")
msg['subject'] = '抢票成功通知!'
msg['from'] = sender
msg['to'] = receiver
s = smtplib.SMTP(host, port)

开始登陆邮箱,并发送邮件

s.login(sender, pwd)
s.sendmail(sender, receiver, msg.as_string())
</pre>


过程演示

  • 执行代码
Python黑科技,以最快速度抢到回家的火车票!
  • 登录12306网站
Python黑科技,以最快速度抢到回家的火车票!
  • 抢票成功
Python黑科技,以最快速度抢到回家的火车票!
  • 等待支付
Python黑科技,以最快速度抢到回家的火车票!
  • 邮件通知
Python黑科技,以最快速度抢到回家的火车票!

总结:

总的来说是成功的,但还不是完全自动化的。不过相比手动抢票还是快很多的。其实网上这种代码很多,只不过因为12306网站的更新大多数不能用了,我也是在前辈们的基础上修改完成的。想要最新源码的同学可以进群:700341555,获取源码!

相关文章

  • Python黑科技,以最快速度抢到回家的火车票!

    又是一年,马上就要回家过年了,还没有买到票的小伙伴们是否已经像热锅上的蚂蚁了无脑的开始找黄牛了? 俗话说的好,求人...

  • 今天,记录下一个小细节

    每次和sophia一起搭车回家时,有两件事一定会经历,一是跟着轻盈的sophia上一辆空车,然后以最快的速度,抢到...

  • 代抢火车票

    还没抢到回家的火车票的朋友看这里。帮忙代刷火车票,地点,时间不限。有意私聊

  • 今夜,我不是归人

    离家远了,过年买火车票确实得抢,还得一骨碌、一骨碌地抢。“抢”字很准确,涵盖了速度和效率、情商和智商。我没抢到回家...

  • 抢票难

    本以为可以很轻松,不,应该说是能抢到回家的火车票,没想到连抢了两天,居然都没抢到票。 哎,回家好难呀...

  • 春节回家,如何快速抢到火车票?

    以前我说过,如果要让90后的员工对你心服口服,你得露两手给她们瞧瞧,你能做她们干不了的事情。让她们愿意跪着上班。这...

  • 回家

    回家的路,千里奔波也幸福。 抢到高铁票,火车票,心里终于安定下来了。

  • 5

    要步入正轨 以最快的速度 嗯

  • 年来了

    年底了,对于打工的人们来说,现在开始有过年的气氛了,因为每年的这个时候要开始抢回家的火车票了,只到抢到了火车票,离...

  • 据说重庆人都准备这样过2017年春节!

    距离2017年春节还有10天 你是否已选购好给家人的礼物? 是否已熬夜抢到回家的火车票? 春节回家 你是否已安排好...

网友评论

    本文标题:Python黑科技,以最快速度抢到回家的火车票!

    本文链接:https://www.haomeiwen.com/subject/rqzkdqtx.html