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中工作日类库Busines Holiday的介绍与使用
Jul 06 Python
Python读取文件内容的三种常用方式及效率比较
Oct 07 Python
python学生管理系统代码实现
Apr 05 Python
python清除字符串中间空格的实例讲解
May 11 Python
python迭代dict的key和value的方法
Jul 06 Python
python实现简单的单变量线性回归方法
Nov 08 Python
Python Unittest根据不同测试环境跳过用例的方法
Dec 16 Python
python使用turtle绘制国际象棋棋盘
May 23 Python
Python3使用PySynth制作音乐的方法
Sep 09 Python
将keras的h5模型转换为tensorflow的pb模型操作
May 25 Python
OpenCV+python实现膨胀和腐蚀的示例
Dec 21 Python
Python使用tkinter制作在线翻译软件
Feb 22 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
利用php来自动调用不同服务器上的flash
2006/10/09 PHP
PHP输出当前进程所有变量/常量/模块/函数/类的示例
2013/11/07 PHP
PHP针对伪静态的注入总结【附asp与Python相关代码】
2017/08/01 PHP
PHP的介绍以及优势详细分析
2019/09/05 PHP
IE和FireFox(FF)中js和css的不同
2009/04/13 Javascript
jQuery代码优化 事件委托篇
2011/11/01 Javascript
JavaScript 监控微信浏览器且自带返回按钮时间
2016/11/27 Javascript
JS实现微信弹出搜索框 多条件查询功能
2016/12/13 Javascript
微信小程序-横向滑动scroll-view隐藏滚动条
2017/04/20 Javascript
详解使用 Node.js 开发简单的脚手架工具
2018/06/08 Javascript
vue+element的表格实现批量删除功能示例代码
2018/08/17 Javascript
vue利用v-for嵌套输出多层对象,分别输出到个表的方法
2018/09/07 Javascript
js核心基础之闭包的应用实例分析
2019/05/11 Javascript
koa router 多文件引入的方法示例
2019/05/22 Javascript
Vue 嵌套路由使用总结(推荐)
2020/01/13 Javascript
vue接口请求加密实例
2020/08/11 Javascript
vue项目开启Gzip压缩和性能优化操作
2020/10/26 Javascript
[01:03:33]Alliance vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
python 参数列表中的self 显式不等于冗余
2008/12/01 Python
从零学python系列之从文件读取和保存数据
2014/05/23 Python
python logging日志模块以及多进程日志详解
2018/04/18 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
2018/04/25 Python
Python(TensorFlow框架)实现手写数字识别系统的方法
2018/05/29 Python
tensorflow学习教程之文本分类详析
2018/08/07 Python
python实现五子棋小程序
2019/06/18 Python
PyTorch实现ResNet50、ResNet101和ResNet152示例
2020/01/14 Python
在python中使用pyspark读写Hive数据操作
2020/06/06 Python
如何理解python对象
2020/06/21 Python
pytorch 查看cuda 版本方式
2020/06/23 Python
Matlab中plot基本用法的具体使用
2020/07/17 Python
Cotton On香港网站:澳洲时装连锁品牌
2018/11/01 全球购物
女方回门宴答谢词
2014/01/14 职场文书
产品销售计划书
2014/05/04 职场文书
户籍证明格式
2014/09/15 职场文书
交通事故赔偿起诉书
2015/05/20 职场文书
如何使用Python实现一个简易的ORM模型
2021/05/12 Python