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标准库之随机数 (math包、random包)介绍
Nov 25 Python
Python标准库sched模块使用指南
Jul 06 Python
python定时关机小脚本
Jun 20 Python
Python面向对象思想与应用入门教程【类与对象】
Apr 12 Python
Python编程学习之如何判断3个数的大小
Aug 07 Python
pycharm实现在子类中添加一个父类没有的属性
Mar 12 Python
Django用户登录与注册系统的实现示例
Jun 03 Python
解决tensorflow/keras时出现数组维度不匹配问题
Jun 29 Python
python自动化测试三部曲之unittest框架的实现
Oct 07 Python
python中remove函数的踩坑记录
Jan 04 Python
详解用selenium来下载小姐姐图片并保存
Jan 26 Python
OpenCV-Python 实现两张图片自动拼接成全景图
Jun 11 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 正则 过滤html 的超链接
2009/06/02 PHP
PHP冒泡排序算法代码详细解读
2011/07/17 PHP
PHP中Memcache操作类及用法实例
2014/12/12 PHP
PHP消息队列用法实例分析
2016/02/12 PHP
php+MySQL实现登录时验证登录名和密码是否正确
2016/05/10 PHP
Extjs学习过程中新手容易碰到的低级错误积累
2010/02/11 Javascript
JavaScript设计模式之适配器模式介绍
2014/12/28 Javascript
js实现拉幕效果的广告代码
2015/09/02 Javascript
bootstrap-treeview自定义双击事件实现方法
2016/01/09 Javascript
Node.js + Redis Sorted Set实现任务队列
2016/09/19 Javascript
浅谈Postman解决token传参的问题
2018/03/31 Javascript
nodejs中方法和模块用法示例
2018/12/24 NodeJs
详解Webpack如何引入CDN链接来优化编译后的体积
2019/06/21 Javascript
vue Treeselect下拉树只能选择第N级元素实现代码
2020/08/31 Javascript
常见的python正则用法实例讲解
2016/06/21 Python
Python简单定义与使用字典dict的方法示例
2017/07/25 Python
python实现读取大文件并逐行写入另外一个文件
2018/04/19 Python
python清除函数占用的内存方法
2018/06/25 Python
python英语单词测试小程序代码实例
2019/09/09 Python
python 和c++实现旋转矩阵到欧拉角的变换方式
2019/12/04 Python
Python Dataframe常见索引方式详解
2020/05/27 Python
Python astype(np.float)函数使用方法解析
2020/06/08 Python
python读取xml文件方法解析
2020/08/04 Python
HTML5轻松实现全屏视频背景的示例
2018/04/23 HTML / CSS
世界领先的在线地板和建筑材料批发商:BuildDirect
2017/02/26 全球购物
广告学专业毕业生自荐信
2013/09/24 职场文书
自我鉴定思想方面
2013/10/07 职场文书
关于逃课的检讨书
2014/01/23 职场文书
玲玲的画教学反思
2014/02/04 职场文书
学习雷锋倡议书
2014/04/15 职场文书
幼儿园区域活动总结
2014/05/08 职场文书
授权委托书格式
2014/07/31 职场文书
关于拾金不昧的感谢信
2015/01/21 职场文书
人才市场接收函
2015/01/30 职场文书
会议通知格式范文
2015/04/15 职场文书
物业公司管理制度
2015/08/05 职场文书