Python实现12306火车票抢票系统


Posted in Python onJuly 04, 2019

Python实现12306火车票抢票系统效果图如下所示:

Python实现12306火车票抢票系统

具体代码如下所示:

import urllib.request as request
  import http.cookiejar as cookiejar
  import re
  import os
  import smtplib
  from email.mime.text import MIMEText
  import time
  user = '' #登陆邮箱
  pwd = ''#邮箱密码
  to = [''] #发送的邮箱
  with open('D:\Python源码\city.txt','r') as f:
    a = f.read()
  station = re.compile(u'\w+:(.+?):(\w+):\d').findall(a)
  dic1 = {}
  for b in range(0, len(station)):
    dic1[station[b][0]] = station[b][1]
  def gethtml(geturl):
    cj = cookiejar.LWPCookieJar()
    cookiejarsupport = request.HTTPCookieProcessor(cj)
    opener = request.build_opener(cookiejarsupport,request.HTTPHandler)
    headers = {
      'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36',
      'Host':'www.12306.cn',
      'Referer':'http://www.12306.cn/opn/lcxxcx/init'
    }
    request.install_opener(opener)
    req = request.Request(url=geturl, headers=headers)
    html = request.urlopen(req).read().decode()
    return html
  def getstation(html):
    fromstation = re.compile(r'from_station_name":"(.+?)","').findall(html)
    tostation = re.compile(r'to_station_name":"(.+?)",').findall(html)
    startime = re.compile(r'"start_time":"(.+?)"').findall(html)
    arrtime = re.compile(r'arrive_time":"(.+?)"').findall(html)
    lishi = re.compile(r'"lishi":"(.+?)",').findall(html)
    webbuy = re.compile(r'"canWebBuy":"(.+?)').findall(html)
    startstation = re.compile(r'start_station_name":"(.+?)"').findall(html)
    endstation = re.compile(r'end_station_name":"(.+?)"').findall(html)
    ruanwo = re.compile((r'"rw_num":"(.+?)",')).findall(html)
    ruanzuo = re.compile((r'"rz_num":"(.+?)"')).findall(html)
    yingwo = re.compile(r'"yw_num":"(.+?)"').findall(html)
    ruanzuo = re.compile(r'"rz_num":"(.+?)"').findall(html)
    yingzuo = re.compile(r'"yz_num":"(.+?)"').findall(html)
    wuzuo = re.compile(r'"wz_num":"(.+?)"').findall(html)
    checi = re.compile(r'station_train_code":"(.+?)"').findall(html)
    datanum = re.compile((r'day_difference":"(.+?)"')).findall(html)
    erdengzuo = re.compile(r'ze_num":"(.+?)",').findall(html)
    num = range(0, len(yingwo))
    for i in num:
      try:
        if int(yingzuo[i]) != 0 or int(erdengzuo[i]) != 0 or int(wuzuo[i] !=0):   #Z108
          print(checi[i], '  二等座:', erdengzuo[i], '  硬座:', yingzuo[i],'  无座:',wuzuo[i])
          if yingwo[i] != '--' or yingzuo[i] != '无':
            msg=MIMEText('火车:'+fromstation[i]+' ->'+tostation[i] +'('+ checi[i]+ ')\n二等座:'+erdengzuo[i]+ '张;硬座:'+ yingzuo[i]+'张;无座:'+wuzuo[i]+ '张!快买去!\n网址:http://www.12306.cn/opn/lcxxcx/init')
            msg['Subject'] = '有票啦!'
            msg['From'] = user
            msg['To'] = ','.join(to)
            s = smtplib.SMTP('smtp.qq.com', timeout = 30) #连接SMTP端口
            s.login(user,pwd)#登陆服务器
            s.sendmail(user,to,msg.as_string())
            s.close()
            print('发送成功')
            print('------------------------------------------------------------')
      except:
        continue
  print('''''
By:王小涛_同?W 
-------------------------------------------------------------- 
  欢迎使用! 
-------------------------------------------------------------- 
''') 
print ('请输入购票类型:(0为成人票  其他为学生票) ') 
leixing = input() 
print('请输入起点:') 
qidian = input() 
try: 
  if dic1[qidian]: 
    qidian = dic1[qidian] 
except: 
  print('起点输入有误!') 
print('请输入终点:') 
zhongdian = input() 
try: 
  if dic1[zhongdian]: 
    zhongdian = dic1[zhongdian] 
except: 
  print('终点输入有误!') 
print('请输入购票年份:') 
year = input()+'-' 
print('请输入购票月份:(2位)') 
month = input()+'-' 
print('请输入购票日期:(2位)') 
date = input() 
date = year + month + date 
if leixing == 0: 
  geturl = 'http://www.12306.cn/opn/lcxxcx/query?purpose_codes=ADULT&queryDate='+date+'&from_station='+qidian+'&to_station='+ zhongdian 
else: 
  geturl = 'http://www.12306.cn/opn/lcxxcx/query?purpose_codes=0X00&queryDate='+date+'&from_station='+qidian+'&to_station='+ zhongdian 
while 1: 
  getstation(gethtml(geturl)) 
  print('火车票监测中...') 
  time.sleep(300) </pre>

总结

以上所述是小编给大家介绍的Python实现12306火车票抢票系统,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
python 实现红包随机生成算法的简单实例
Jan 04 Python
python正则表达式re之compile函数解析
Oct 25 Python
python中的闭包函数
Feb 09 Python
matplotlib.pyplot画图 图片的二进制流的获取方法
May 24 Python
python实现自主查询实时天气
Jun 22 Python
pygame游戏之旅 调用按钮实现游戏开始功能
Nov 21 Python
python调用摄像头拍摄数据集
Jun 01 Python
对python中GUI,Label和Button的实例详解
Jun 27 Python
关于numpy数组轴的使用详解
Dec 05 Python
使用python接受tgam的脑波数据实例
Apr 09 Python
PyQt 如何创建自定义QWidget
Mar 24 Python
用Python可视化新冠疫情数据
Jan 18 Python
如何利用Pyecharts可视化微信好友
Jul 04 #Python
python 获取等间隔的数组实例
Jul 04 #Python
python 中pyqt5 树节点点击实现多窗口切换问题
Jul 04 #Python
Python机器学习算法库scikit-learn学习之决策树实现方法详解
Jul 04 #Python
Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法
Jul 04 #Python
Python+opencv 实现图片文字的分割的方法示例
Jul 04 #Python
pandas 使用均值填充缺失值列的小技巧分享
Jul 04 #Python
You might like
phpmailer中文乱码问题的解决方法
2014/04/22 PHP
PHP时间相关常用函数用法示例
2020/06/03 PHP
解密效果
2006/06/23 Javascript
DOM相关内容速查手册
2007/02/07 Javascript
十个优秀的Ajax/Javascript实例网站收集
2010/03/31 Javascript
event.X和event.clientX的区别分析
2011/10/06 Javascript
js onkeypress与onkeydown 事件区别详细说明
2012/12/13 Javascript
js 判断一个元素是否在页面中存在
2012/12/27 Javascript
图标线性回归斜着移动到指定的位置
2013/08/16 Javascript
基于jQuery实现下拉框
2014/11/24 Javascript
javascript实现单击和双击并存的方法
2014/12/13 Javascript
JS实现点击复选框将按钮或文本框变为灰色不可用的方法
2015/08/11 Javascript
JS中的二叉树遍历详解
2016/03/18 Javascript
纯javaScript、jQuery实现个性化图片轮播【推荐】
2017/01/08 Javascript
Angular 2父子组件数据传递之@ViewChild获取子组件详解
2017/07/04 Javascript
详解如何在webpack中做预渲染降低首屏空白时间
2018/08/22 Javascript
angular ng-model 无法获取值的处理方法
2018/10/02 Javascript
分享5个小技巧让你写出更好的 JavaScript 条件语句
2018/10/20 Javascript
vue-cli+axios实现文件上传下载功能(下载接收后台返回文件流)
2019/05/10 Javascript
微信小程序实现二维码签到考勤系统
2020/01/16 Javascript
swiper4实现移动端导航栏tab滑动切换
2020/10/16 Javascript
python 打印出所有的对象/模块的属性(实例代码)
2016/09/11 Python
python将excel转换为csv的代码方法总结
2019/07/03 Python
Python日志无延迟实时写入的示例
2019/07/11 Python
Python如何将图像音视频等资源文件隐藏在代码中(小技巧)
2020/02/16 Python
pandas分批读取大数据集教程
2020/06/06 Python
下面关于"联合"的题目的输出是什么
2013/08/06 面试题
初一地理教学反思
2014/01/16 职场文书
岗位职责怎么写
2014/03/14 职场文书
婚庆公司计划书
2014/09/15 职场文书
十岁生日答谢词
2015/01/05 职场文书
调任通知
2015/04/21 职场文书
幼儿园班级管理心得体会
2016/01/07 职场文书
大学生暑期社会实践的个人总结!
2019/07/17 职场文书
zabbix自定义监控nginx状态实现过程
2021/11/01 Servers
Python可视化神器pyecharts之绘制地理图表练习
2022/07/07 Python