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数据类型_字符串常用操作(详解)
May 30 Python
Python实现的简单排列组合算法示例
Jul 04 Python
Python 读写文件的操作代码
Sep 20 Python
对Python3中bytes和HexStr之间的转换详解
Dec 04 Python
Python安装与基本数据类型教程详解
May 29 Python
在python中用print()输出多个格式化参数的方法
Jul 16 Python
python matplotlib库绘制散点图例题解析
Aug 10 Python
解决python有时候import不了当前的包问题
Aug 28 Python
利用 Flask 动态展示 Pyecharts 图表数据方法小结
Sep 04 Python
基于python判断目录或者文件代码实例
Nov 29 Python
MNIST数据集转化为二维图片的实现示例
Jan 10 Python
详解解决jupyter不能使用pytorch的问题
Feb 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
mysql 全文搜索 技巧
2007/04/27 PHP
php递归获取目录内文件(包含子目录)封装类分享
2013/12/25 PHP
thinkphp微信开发(消息加密解密)
2015/12/02 PHP
YII框架实现自定义第三方扩展操作示例
2019/04/26 PHP
laravel异步监控定时调度器实例详解
2019/06/21 PHP
php swoole多进程/多线程用法示例【基于php7nts版】
2019/08/12 PHP
JQuery 技巧和窍门整理(8个)
2010/04/22 Javascript
jQuery-onload让第一次页面加载时图片是淡入方式显示
2012/05/23 Javascript
Jquery通过Ajax访问XML数据的小例子
2013/11/18 Javascript
js检验密码强度(低中高)附图
2014/06/05 Javascript
浅谈Javascript 数组与字典
2015/01/29 Javascript
jQuery mobile转换url地址及获取url中目录部分的方法
2015/12/04 Javascript
深入理解JS函数的参数(arguments)的使用
2016/05/28 Javascript
JS二叉树的简单实现方法示例
2017/04/05 Javascript
解决BootStrap Fileinput手机图片上传显示旋转问题
2017/06/01 Javascript
了解JavaScript表单操作和表单域
2019/05/27 Javascript
vue+element搭建后台小总结 el-dropdown下拉功能
2020/04/10 Javascript
详解关闭令人抓狂的ESlint 语法检测配置方法
2019/10/28 Javascript
vue实现设置载入动画和初始化页面动画效果
2019/10/28 Javascript
微信小程序修改checkbox的样式代码实例
2020/01/21 Javascript
js中位数不足自动补位扩展padLeft、padRight实现代码
2020/04/06 Javascript
[01:10:03]OG vs EG 2018国际邀请赛淘汰赛BO3 第三场 8.23
2018/08/24 DOTA
Python 专题六 局部变量、全局变量global、导入模块变量
2017/03/20 Python
Python基于win32ui模块创建弹出式菜单示例
2018/05/09 Python
详解python中的生成器、迭代器、闭包、装饰器
2019/08/22 Python
Python基于Socket实现简单聊天室
2020/02/17 Python
PyCharm2020.1.1与Python3.7.7的安装教程图文详解
2020/08/07 Python
HTML 5 input placeholder 属性如何完美兼任ie
2014/05/12 HTML / CSS
澳大利亚香水在线:Price Rite Mart
2017/12/28 全球购物
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
环境工程大学生个人的自我评价
2013/10/08 职场文书
精通CAD能手自荐书
2014/01/31 职场文书
三方股份合作协议书
2014/10/13 职场文书
庆六一开幕词
2015/01/29 职场文书
中考学习决心书
2015/02/04 职场文书
2016年班主任新年寄语
2015/08/18 职场文书