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判断某个用户对某个文件的权限
Oct 13 Python
python实现图片处理和特征提取详解
Nov 13 Python
Python模块搜索路径代码详解
Jan 29 Python
Python Tkinter实现简易计算器功能
Jan 30 Python
python增加矩阵维度的实例讲解
Apr 04 Python
python爬取基于m3u8协议的ts文件并合并
Apr 26 Python
Python生成指定数量的优惠码实操内容
Jun 18 Python
Python处理时间日期坐标轴过程详解
Jun 25 Python
Django对接支付宝实现支付宝充值金币功能示例
Dec 17 Python
python 定义类时,实现内部方法的互相调用
Dec 25 Python
PyTorch 如何检查模型梯度是否可导
Jun 05 Python
Python序列化模块JSON与Pickle
Jun 05 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
动态网站web开发 PHP、ASP还是ASP.NET
2006/10/09 PHP
Laravel 5 框架入门(三)
2015/04/09 PHP
如何修改yii2.0自带的user表为其它的表
2017/08/01 PHP
IE6不能修改NAME问题的解决方法
2010/09/03 Javascript
JavaScript中创建对象和继承示例解读
2014/02/12 Javascript
动态添加删除表格行的js实现代码
2014/02/28 Javascript
js解析json读取List中的实体对象示例
2014/03/11 Javascript
js下将阿拉伯数字每三位一逗号分隔(如:15000000转化为15,000,000)
2014/06/02 Javascript
一段非常简单的js判断浏览器的内核
2014/08/17 Javascript
jQuery实现强制cookie过期方法汇总
2015/05/22 Javascript
使用Meteor配合Node.js编写实时聊天应用的范例
2015/06/23 Javascript
基于JS实现的笛卡尔乘积之商品发布
2016/05/13 Javascript
jquery append与appendTo方法比较
2017/05/24 jQuery
vue的基本用法与常见指令
2017/08/15 Javascript
p5.js 毕达哥拉斯树的实现代码
2018/03/23 Javascript
Angular8 Http拦截器简单使用教程
2019/08/20 Javascript
js回到页面指定位置的三种方式
2020/12/17 Javascript
Python GAE、Django导出Excel的方法
2008/11/24 Python
Python IDE PyCharm的基本快捷键和配置简介
2015/11/04 Python
利用python实现数据分析
2017/01/11 Python
Python实现读取TXT文件数据并存进内置数据库SQLite3的方法
2017/08/08 Python
Python 图像处理: 生成二维高斯分布蒙版的实例
2019/07/04 Python
python如何实现数据的线性拟合
2019/07/19 Python
django将数组传递给前台模板的方法
2019/08/06 Python
Pycharm创建python文件自动添加日期作者等信息(步骤详解)
2021/02/03 Python
HTML5实现锚点时请使用id取代name
2013/09/06 HTML / CSS
HTML5实现动画效果的方式汇总
2016/02/29 HTML / CSS
圣彼得堡鲜花配送:Semicvetic
2020/09/15 全球购物
解释一下ArrayList Vector和LinkedList的实现和区别
2013/04/26 面试题
高一新生军训方案
2014/05/12 职场文书
国庆节演讲稿范文2014
2014/09/19 职场文书
离婚起诉书范文2015
2015/05/19 职场文书
焦点访谈观后感
2015/06/11 职场文书
redis 存储对象的方法对比分析
2021/08/02 Redis
golang操作rocketmq的示例代码
2022/04/06 Golang
Android学习之BottomSheetDialog组件的使用
2022/06/21 Java/Android